The code:
console.log(start)
reads, Thu Mar 01 2018 00:00:00 GMT+0530 (India Standard Time)
I want it a new object start_NY which will read Thu Mar 01 2018 00:00:00 w.r.t. America/New_York timezone. Something like Thu Mar 01 2018 00:00:00 GMT-0500.
I used a script:
start_ny = new Date('Thu Mar 01 2018 00:00:00 GMT-0500');
But that reads, Thu Mar 01 2018 10:30:00 GMT+0530 (India Standard Time), which is actually converting the date into Indian Standard Time, instead of giving me a time from New_York timezone.
The format should be same as that of the existing one. How can I do that?
Try with this following code and MDN resources
new Date().toLocaleString('en-US', { timeZone: 'America/New_York' })
// Date Format
new Date().toDateString('en-US', { timeZone: 'America/New_York' });
Related
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
I'm trying to format an string date that looks like this:
Tue Mar 13 2018 00:00:00 GMT-0600 (Mountain Daylight Time)}
to:
"2018-03-13T00:00:00",
Can anyone tell me what I'm missing or need to add? Thanks a lot in advance!
let someDate = 'Tue Mar 13 2018 00:00:00 GMT-0600 (Mountain Daylight Time)}';
console.log(someDate.replace(/T.+$/, "T00:00:00"));
let someDate = 'Tue Mar 13 2018 00:00:00 GMT-0600 (Mountain Daylight Time)';
console.log(new Date(someDate).toISOString().replace(/T.+$/, "T00:00:00"));
someDate.toISOString() will give you the date in ISO format
> new Date('2015-1-1')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-01-1')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-1-01')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
// Yet...
> new Date('2015-01-01')
Wed Dec 31 2014 19:00:00 GMT-0500 (EST)
// Similarly:
> new Date('2015-1-10')
Sat Jan 10 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-01-10')
Fri Jan 09 2015 19:00:00 GMT-0500 (EST)
Can't figure out why this is happening (Chrome 39). Is it related to octal parsing?
Firefox only accepts new Date('2015-01-10'), and returns what I expect: Date 2015-01-10T00:00:00.000Z
Found the answer in a related question; it appears Chrome parses the YYYY-MM-DD format as UTC time, then converts it the local timezone. So, 2015-01-01 00:00:00 in UTC is Dec 31 in EST.
See Inconsistencies when creating new date objects:
It looks like the form '1979-04-05' is interpreted as a UTC date (and then that UTC date is converted to local time when displayed).
Apparently, a possible cross browser solution is to replace the dashes with slashes to force using local time:
new Date('2015-01-10'.replace(/-/g, '/'))
I am unsure of your problem since My chrome(39.0.2171.99) gives me Jan 01 in all case. But having said this, I would like to point out that you should probably use
new Date(2015,1,1)
This is how JS Date is supposed to be initialised.
by using:
Date.parse('2015-01-01');
it gives output: Thu Jan 01 2015 05:00:00 GMT+0500 (Pakistan Standard Time)
but i want just like this: Thu Jan 01 2015
(moreover i am using it in morris.js chart)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Conversion_getter
new Date('2015-01-01').toDateString();
I got one weird issue with Date object initialization. And wondering if someone can explain why..
var exp1 = new Date('2014-10-17');
var exp2 = new Date(2014,9,17);
var exp3 = new Date('17 Oct 2014');
console.log(exp1);
console.log(exp2);
console.log(exp3);
Results:
Thu Oct 16 2014 18:00:00 GMT-0600 (MDT) // 16th?
Fri Oct 17 2014 00:00:00 GMT-0700 (MST) // Why GMT -7
Fri Oct 17 2014 00:00:00 GMT-0600 (MDT) // The only one that works as expected
Why are these three Date objects so different?
The first date is treated as GMT since no time zone offset is provided. When logged out it shows the time in your local timezone. Adding an offset (exp4 below), I get the date expected.
var exp1 = new Date('2014-10-17');
var exp2 = new Date(2014,9,17);
var exp3 = new Date('17 Oct 2014');
var exp4 = new Date('2014-10-17z-0500');
Results:
Thu Oct 16 2014 19:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
I am not sure about exp2 for you, but suspect it has something to do with daylight savings time and that you live in an area that does not observe daylight savings (Arizona?).
Edit: this seems to be browser specific. The results above were generated in Chrome while in IE 11, exp4 was an invalid date. For IE 11 I had to use this format:
var exp4 = new Date('2014-10-17T00:00-05:00');