Convert a UTC ISO string to a UTC Date object - javascript

I have a string in ISO format 2020-03-11T21:00:00Z. I need help with converting this string to a Date object but keep it as UTC timezone. The expected output is Wed Mar 11 2020 21:00:00 GMT+0000 (UTC).
I've tried moment.utc('2020-03-11T21:00:00Z').toDate() but the output was in my local timezone: Thu Mar 12 2020 05:00:00 GMT+0800 (Malaysia Time).
Thanks

Related

Cannot parse DateTime object from date string with country and daylight time

I cannot parse this string as a DateTime object with luxon. It always comes back as invalid.
Thu Feb 23 2023 16:00:00 GMT+1300 (New Zealand Daylight Time)
It hasnt worked with any of these fromISO, fromHTTP, fromRFC28822 so I suspect it should be converted into another format first.
DateTime('Thu Feb 23 2023 16:00:00 GMT+1300 (New Zealand Daylight Time)').fromJSDate() worked.

What is JavaScript's default Date format?

What is the name of this format?
Wed Jul 06 2022 14:42:13 GMT-0400 (Eastern Daylight Time)
This format is the default format that is output "Independent of input format" from new Date().
source - https://www.w3schools.com/js/js_date_formats.asp w3schools explains the format but fails to mention what it's called before getting into other ISO format types.
How do you turn a date into that format using Moment.js?
Example: 2022-07-15T00:00:00.000Z or 2015-03-25
console.log(moment(new Date()).toISOString());
// 2022-07-06T19:08:36.670Z
console.log(moment(new Date()).toString());
// Wed Jul 06 2022 15:08:36 GMT-0400
console.log(new Date());
// Wed Jul 06 2022 15:08:36 GMT-0400 (Eastern Daylight Time)
console.log(new Date().toString());
// Wed Jul 06 2022 15:08:36 GMT-0400 (Eastern Daylight Time)
You can see that moment(new Date()).toISOString() and moment(new Date()).toString() DO NOT output that same format as the default JS format.
moment(new Date()).toString() is close, but is still missing the (Eastern Daylight Time) which is part of the default.
What is the name of this format?
It's not ISO, it's a format specified in ECMA-262, read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString
To convert it to ISO use date.toISOString()
Btw. don't use w3schools.com this website has major errors
How do you turn a date into that format using Moment.js?
ISO: moment(new Date()).toISOString()
2015-03-25: moment(new Date()).format('yyyy-MM-dd')
ECMA-262: moment(new Date()).toDate().toString()
P.S. I would consider using a different library as moment.js isn't getting updates anymore, but if you don't have a choice it's fine.
Answer:
console.log(moment(new Date()).toDate());
That was way harder to find than it should have been.
console.log(moment(new Date()).toDate());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)
console.log(new Date());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)
console.log(new Date().toString());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)
Bonus:
console.log(moment(1234, moment.ISO_8601, true).isValid());
// true
console.log(1234 instanceof Date)
// false
console.log(new Date() instanceof Date);
// true

Convert javascript date to json date format

I'm working with an API which expects json date format.
I need to convert my javascript date
Sat Jan 17 1970 07:28:19 GMT+0100 (Romance Standard Time)
To json date format
/Date(1405699200)/
Is anything of the following ok?
console.log(new Date('Sat Jan 17 1970 07:28:19 GMT+0100 (Romance Standard Time)').toJSON());
console.log(new Date('Sat Jan 17 1970 07:28:19 GMT+0100 (Romance Standard Time)').valueOf());
/Date(1405699200)/
Here 1405699200 is your date in epoch format.
You can do this :
var myDate = new Date("July 1, 1978 02:30:00"); // Your timezone!
var myEpoch = myDate.getTime()/1000.0;
document.write(myEpoch);
To convert human readable date format to Epoch format : https://www.epochconverter.com/programming/#javascript

Strange date parsing issue

I'm loading some dates comming from my database into a HTML table in a string format. The string looks like 31-AUG-13 I'm parsing this string into a date object using the below code:
var paymentDate = $(this).find('td.paymentDate').text();
var test = $.datepicker.parseDate('d-M-y', paymentDate);
Everything is ok so far and I'm getting this date object: Date {Sat Aug 31 2013 00:00:00 GMT+0300 (FLE Standard Time)} But once the year is bigger than 2023. In my case 31-JAN-24 and so on it is turning to 1924 and not 2024, so I'm getting these date objects:
Date {Thu Jan 31 1924 00:00:00 GMT+0200 (FLE Daylight Time)}
Date {Fri Feb 29 1924 00:00:00 GMT+0200 (FLE Daylight Time)}
Date {Mon Mar 31 1924 00:00:00 GMT+0300 (FLE Standard Time)}
Date {Wed Apr 30 1924 00:00:00 GMT+0300 (FLE Standard Time)}
And so on. My question is regarding this strange issue. Is there a way to declare the year range and why it is going back to 1900 in the case when the year is bigger than 2023?
This teaches a lesson always use year in full format same was case with y2k problem. Convert date from database in to yyyy format then use it.

invalid date/time string: Sun Jun 03 2012 00:00:00 GMT+0100 (GMT Daylight Time) CFML

I'm trying to query to database using information from a html input. The format of the date is:
Date {Sun Jun 03 2012 00:00:00 GMT+0100 (GMT Daylight Time)}
In the database query I used
parseDateTime( arguments.start )
.. where arguments.start represents the date format shown above. But when I try to run the query, I get the response below from the web page:
invalid date/time string: Sun Jun 03 2012 00:00:00 GMT+0100 (GMT
Daylight Time)
Not sure what format you have there, but that is not a valid ISO 8601 time format. All ISO times formats are always GMT and are one of the following formats:
1994-11-05T13:15:30Z
1994-11-05T08:15:30-05:00
I would check whatever is giving you that string value.

Categories

Resources