MomentJS ISO date formatting - javascript

Date before ISO conversion it was like 'Sat Oct 22 2016 00:18:38 GMT+0530 (IST)' But after convert using moment i.e. moment().toISOString() It gives
result output like '2016-10-21T18:48:00.337Z', here it gives date 21.
why?

Related

Date conversion issue in JS

I have a date in local time zone which is Mon Jan 13 2020 00:00:00 GMT+0530 (India Standard Time).
I need this
Jan 13 2020
date in UTC. when i converted using
moment(
moment(start_date).format('mm-dd-yyyy'),
).Unix();
i am receiving a UNIX time stamp corresponding to the date Jan 12 2020 in UTC.
what i need is for Jan 13 2020 get a UTC timestamp of date Jan 13 2020 instead of getting Jan 12 2020. Time values are not important.
Get the UTC timestamp like this:
moment.utc('Jan 13 2020').format('X')
Above will give you the UTC timestamp in seconds.
If you want milliseconds, use x instead of X.
Breaking down your statement:
moment(start_date)
does not provide the parse format to moment.js so you will get a warning to that effect. In this case, moment.js will fallback to the built–in parser as if you'd written:
moment(Date.parse(start_date))
so you are hoping the built–in parser parses it correctly.
Then there is:
.format('mm-dd-yyyy')
which will return a date string in that format or Invalid Date if the parser couldn't parse the string.
However, likely you have used the wrong tokens so the result will be something like "30-mo-yyyy":
mm is two digit minute, if you want two digit month then use MM
dd is two letter day name, if you want two digit day in month use DD
yyyy is not a valid token, if you want the four digit year then use YYYY
So the format tokens should be:
.format('MM-DD-YYYY')
The result is then passed to moment, again without specifying the input format so again moment will show a warning and use the built–in parser again with the same potential for errors or invalid Dates. If parsed correctly, it will return a Date for the local timezone for the Date.
.Unix()
Is not a valid moment method, likely you want .unix(), which produces a UNIX timestamp (time value) for the equivalent UTC time. If the host system is set to +5:30 then the UTC time will be 5:30 earlier (and if it's before 5:30 am local, the date will be the day before).
If you want to parse the string correctly and get a date for the start of the day, then do:
moment(start_date, 'ddd MMM DD YYYY HH:mm:ss ZZ').startOf('day').unix()
Or create a formatted string however you like. A much more common format is YYYY-MM-DD, so consider:
let start_date = 'Mon Jan 13 2020 00:00:00 GMT+0530 (India Standard Time)';
// Get UNIX timestamp for start of day
console.log(
moment(start_date, 'ddd MMM DD YYYY HH:mm:ss ZZ').startOf('day').unix()
);
// Get formatted date string
console.log(
moment(start_date, 'ddd MMM DD YYYY HH:mm:ss ZZ').format('YYYY-MM-DD')
);
console.log(
moment(start_date, 'ddd MMM DD YYYY HH:mm:ss ZZ').format('DD MMM YYYY')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Convert Date format YYYY-MM-DDThh:mm

I request a API, this bellow format is not accept:
Thu Apr 12 2018 00:00:00 GMT+0800 (CST)
the
"start_time":["Date format error. Please ues this type format:YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]。"
part of my request:
Part of my request data:
Content-Disposition: form-data; name="start_time"
Thu Apr 12 2018 00:00:00 GMT+0800 (CST)
------WebKitFormBoundaryPtZBgjhBEahHBOLY
Content-Disposition: form-data; name="end_time"
Thu May 31 2018 00:00:00 GMT+0800 (CST)
I have two questions:
Does the Thu Apr 12 2018 00:00:00 GMT+0800 (CST) is one of the Date format? And then there is how many types of Date format?
How can I convert the example Date to this format YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]?
You don't say how you get the string. If you have a Date object, you can use toISOString to generate a timestamp in the required format. To answer your questions:
Does the Thu Apr 12 2018 00:00:00 GMT+0800 (CST) is one of the Date format?
No. The ECMAScript Date object is only required to parse the subset of ISO 8601 noted in the language specification, ECMA-262. Support for any other format is implementation dependent.
In general, you shouldn't rely on the built-in parser. It's much better to either write your own parser, or if you need to support a number of formats, use a library. There are plenty of good ones to choose from, but the bottom line is that you must know the timestamp format in order to be sure of correctly parsing it, whether you use a library or write your own parser.
And then there is how many types of Date format?
A Date object doesn't have any format, it is simply a time value that represents a moment in time in UTC plus some fairly simplistic methods. The format of toString is implementation dependent, however toISOString is compliant with ISO 8601. But to use it requires a Date object, so you'd have to parse the string to a Date to use it.
How can I convert the example Date to this format YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]?
You can either parse it to a Date (using your own parser or a library) then use toISOString (or library equivalent), or just reformat the string, e.g.
var s = 'Thu Apr 12 2018 00:00:00 GMT+0800 (CST)';
function toISOString(s) {
var months = {jan:'01',feb:'02',mar:'03',apr:'04',may:'05',jun:'06',
jul:'07',aug:'08',sep:'09',oct:'10',nov:'11',dec:'12'};
var b = s.split(' ');
return b[3] + '-' +
months[b[1].toLowerCase()] + '-' +
('0' + b[2]).slice(-2) + 'T' +
b[4] + b[5].substr(3);
}
console.log(toISOString(s));

Javascript convert date string to date and time

I have a date string in the below format,
Wed Feb 21 2018 09:20:46 GMT+0530 (IST)
using Javascript want to convert the date string like this, i.e, date and time separated with 'T'.
2018-02-21T09:20:46.66
You can use Moment.js
moment("Wed Feb 21 2018 09:20:46 GMT+0530 (IST)").format('YYYY-MM-DDTHH:MM:SS')
This Code will do it.
var date = new Date()
date.toJSON().slice(0,10).replace(/-/g,'-') + "T" +
date.toJSON().split('T')[1].split('.')[0]
It looks like you need a Date String to be converted to ISO Format. You can use the toISOString() method as follows:
var event = new Date('Wed Feb 21 2018 09:20:46 GMT+0530 (IST)');
console.log(event.toISOString());
Result is "2018-02-21T03:50:46.000Z"
You can use .slice() method to trim the last Z if you do not require it. You'd then have to use event.toISOString().slice(0,-1) to trim the last character in the returned ISO String.

Convert date MongoDB / Javascript, but Nan

I have a little problem with a date conversion in javascript.
I have a date format like this one Wed Mar 09 10:32:14 CET 2016 from a Mongo database. When I want to get the month for example, I do this:
d=new Date('Wed Mar 09 10:32:14 CET 2016');
alert(d.getMonth());
But It does not work (NaN in the alert).
I know that a format like Wed Mar 09 2016 10:32:14 GMT will work, but the Database returns me Wed Mar 09 10:32:14 CET 2016.
I want to avoid the date format transformation.
You have to use the format "Mon, 25 Dec 1995 13:30:00 GMT", the Date constructor allow use date as String but with the format specified before, the Date constructor use the Date.parse to parse the string to date, read further details about the Date.parse. Store the date into mongodb in timestamps.

Converting string to datetime in javascript

I am convert the string to datetime using Date.parse and new Date(). For example if the string is "2/5/2012" then
var date=new Date(Date.parse("2/5/2012")); // Sun Feb 5 00:00:00 PST 2012
var date=new Date(Date.parse("2012/15/3")); //Sun Mar 3 00:00:00 PST 2013
Here my doubt is the string that i am giving takes the below format
"2/5/2012" - "mm/dd/yyyy"
"2012/15/3" - "yyyy/mm/dd"
I Want to know what are the format that i can use to define the date in string.
can i define string like this "3/2012/4"
Thanks in advance

Categories