What kind of format is this date in? - javascript

2018-06-04T01:00:45.500Z
Moment.js returns a date object when calling moment(), but sometimes returns this timestamp.
How can I make sure I'm receiving a timestamp like this and not a date object?
For example, output of calling console.log(moment()):

Did you try this?
moment().format('YYYY-MM-DDTHH:mm:ssZ');
This returns an output in this format :
2018-06-04T12:49:53+10:00

That format is ISO, is the universal format to represent a date in javascript. It's the same format that you get from Date.toISOString()
You can get this date format as follow
console.log(moment().toISOString())
reference https://momentjs.com/docs/#/displaying/as-iso-string/

Related

how to format date pulled from database(inserted as new Date() )

I'm not so sure what's the proper way to insert date into the database, but I'm using new Date().
So I get date format like this when I query from the database:
2021-09-24T12:38:54.656Z
Now I realized that date format is not so user-friendly. So I'm trying to convert it if possible standard readable format like this:
Sept 25 2015, 8:00 PM
I tried using toLocaleString() to my date pulled from db but it won't work probably if I got it correctly pulled date from the db is already a string?
Is there a workaround for this so that I don't need to change how I enter date to my db?
const date = moment("2021-09-24T12:38:54.656Z").format('MMM D YYYY, h:mm a')
console.log(date)
<script src="https://momentjs.com/downloads/moment.min.js"></script>
use momentjs
You can find format method and apply as you desire
https://momentjs.com/
I think it is better to store in DB as UTC 00 value and you are doing it right now.
When you retrieve it, you will be getting a string value something like, 2021-09-24T12:38:54.656Z. On the UI you can easily convert it to date variable in JS using,
const dateVar = new Date("2021-09-24T12:38:54.656Z");
console.log(dateVar.toLocaleString());
If you want more date formatting other than the inbuild solutions like toLocaleString you can use date libraries like moment.js
You'd need to create a Date object from the timestamp string like this.
let date = new Date('2021-09-24T12:38:54.656Z');
Then you can use toLocaleString, toLocaleDateString, toDateString, or toString as you see fit.

How do I format a date as ISO date(mongodb) using moment.js?

I'm trying to convert below-mentioned date to ISO format(MongoDB)
var d = { storedDate: '26/06/2020 05:55:29 PM' };
I'm however unable to find the parameter that I need to use to get it in the format which I want. I tried the below piece of code.
moment(d.storedDate).format("YYYY-MM-DD HH:mm Z");
How can I get it as ISODate("2020-06-26T17:55:29.274Z")
Please advice
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toISOString() will return ISO date only in UTC that you need for MongoDb. You need to provide the input date format as well.
If you want to store proper Date object, use
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toDate()
You should not store date/time values as strings, use proper data type.

Converting to same date format using format of Moment JS gives invalid date

I have a react application where at one place we are converting date using moment like this
moment("08/19/1994", 'DD/MM/YYYY', true).isValid()
this returns the date as "19/08/1994 and this value is stored in redux store.
When the component is again refreshed, this conversion happens again and this time it works like this
moment("19/08/1994", 'DD/MM/YYYY', true).isValid() //invalid date.
So if we are converting same date format it is giving the error So is there a way we can check the format or git rid of this invalid date using any other method.
You should convert the date only the first time you store it in redux, not in the react component.
isValid() returns boolean based on the date validity
Use format() to convert date to 'DD/MM/YYYY'
console.log(moment("08/19/1994", 'MM/DD/YYYY').format('DD/MM/YYYY'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

JavaScript: Convert date string mm/dd/yyyy to date mm/dd/yyyy

I receive a string that comes in from SQLserver with the format:
'mm/dd/yyyy' or CONVERT(VARCHAR(10), [ActivityDate], 101)
I need to convert that string value, to an actual date value, but keeping the same date format:
mm/dd/yyyy
I need to format the date is because it comes from SQL server in format '2015-02-18 00:00:00.000' to a page that uses angularJS sort and fileter taken from this example: https://scotch.io/tutorials/sort-and-filter-a-table-using-angular
in my table, I have a date column that uses format mm/dd/yyyy, when I type 12/21/2015 I get nothing from the filter even though there are records with this date. The reason why the filter does not work, is because the date even thouhg it displays as mm/dd/yyyy, still has the fromat from sql. The filter works when I type the date 2015-12-21, but that would be misleading to the user.
Does this makes sense?
For your case, you can use new Date() constructor that implicitly calls Date.parse()
new Date('02/21/1994')
//> Date 1994-02-20T21:00:00.000Z
+1 to Claies, i recommend to use moment.js too

How to convert time and date string to ISO format

I have two inputs, a time and a date input. I'm trying to format them as an ISO string to send to the backend using moment.js.
This is what I have so far 01:00 2016-01-01, I need to format or convert that to ISO. Is there a way to do it using Moment?
To convert ISO I recommend the more standard
date.format();
or
JSON.stringify(yourDate)
or if you prefer momentjs:
var date = moment();
date.toISOString();
or
moment(yourDate).format('MM/DD/YYYY'); // <- your custom format string
To know what are the momentjs formatting rules start reading here
Assuming you are referring to ISO8601 and momentjs (2.10.6), I currently do it like this
var example = momentObject.format("YYYY-MM-DD[T]HH:mm:ss");
You need to use moment's parse function to first create the correct moment object from the data that you have (assuming a 24-hour clock, and month listed before the days):
var myMoment = moment("01:00 2016-01-01", "HH:mm YYYY-MM-DD");
Then you can use moment's format function to output the date in the ISO format that you want. Note that calling the format function without any parameters will output ISO 8601 by default:
myMoment.format();
See the moment docs for more info here.
Hope this helps!

Categories

Resources