Why is new Date() converting the timezone? I'd like my date to be the same as the string I provide, so 00:30 and not 10:30.
>>> new Date("2015-04-11T00:30:00");
Sat Apr 11 2015 10:30:00 GMT+1000 (AEST)
You passed the date in ISO form into the constructor "2015-04-11T00:30:00".
That means your browser interprets that not as local time but as UTC. Date.toString however uses your local time. If you want to use UTC time call .toUTCString or better yet .toISOString.
Related
I have a typescript method that expects a Date object and it converts into a local timezone using moment. Now I want to write a unit test for that and to accomplish that I need a Date object in UTC format.
The problem is when I do new Date() to get date object it returns date with local timezone
Wed Apr 15 2020 11:38:10 GMT+0530 (India Standard Time)
and since the zone already local my method won't convert it into local again.
convertUTCtoLocal(utcDate: Date): Date {
return new Date(moment.utc(utcDate).local().format());
}
I know there are methods like .toISOString(), .toGMTString() but these return a string I want a Date object
Any idea, what can be done.
I have a scenario where I am using AngularJS to read date. Interestingly it decreases my date value by one.
Why is this happening?
new Date("2016-01-06T00:00:00")
give me result as
Tue Jan 05 2016 16:00:00 GMT-0800 (Pacific Standard Time)
This is because when you use the new Date() in the JavaScript, it converts and prints the date in browsers timezone.
So if you print:
new Date("2016-01-06T00:00:00-0800")
You will get the actual output you want, because of the -0800 difference between your time zone (determined by the browser) and the UTC time.
The UTC time zone is used to interpret arguments in ISO 8601 format that do not contain time zone information (note that ECMAScript 2015 specifies that date time strings without a time zone are to be treated as local, not UTC).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
Since your date string appears to lack one, JS assumed it's UTC time. The result you see is the same moment in time, offset to your timezone. All you need to do is provide timezone data to the string you're parsing.
It is because the date is taking your browser's timezone in to account, which in your case is in PST. It does the same to me, in EST:
test = new Date("2016-01-06T00:00:00")
Tue Jan 05 2016 19:00:00 GMT-0500 (EST)
You can still obtain the time in UTC by using any of the .getUTC* functions, like so:
test.getUTCDate();
6
Would someone explain why formatting the same dateString differently gives a different date?
> new Date("04/08/1984")
<· Sun Apr 08 1984 00:00:00 GMT-0600 (Mountain Daylight Time)
> new Date("1984-04-08")
<· Sat Apr 07 1984 18:00:00 GMT-0600 (Mountain Daylight Time)
When you create a new Date object passing a dateString parameter to the constructor, it gets parsed using the Date.parse() method. Now, quoting from the MDN documentation (emphasis mine):
Differences in assumed time zone
Given a date string of "March 7, 2014" (or "03/07/2014"), parse() assumes a local time zone, but given an ISO format such as "2014-03-07" it will assume a time zone of UTC. Therefore Date objects produced using those strings will represent different moments in time unless the system is set with a local time zone of UTC.
Therefore, since that your are giving the second string in the ISO format, and your local time zone is UTC+6, you're getting a date which is six hour behind yours, because it gets calculated as UTC+0. In fact:
Apr 07 1984 18:00:00 = Apr 08 1984 00:00:00 - 06:00:00
Mystery solved!
Your problem is that you are adding 0 before the numbers in "1984-04-08". Try the following:
new Date("1984-4-8")
document.write(new Date("04/08/1984"));
document.write("<br>");
document.write(new Date("1984-4-8"));
I am creating new date objects in javascript and seeing some inconsistencies depending on whether I use the dateString parameter vs the year/month/day integer parameters.
Here's an example:
var dt1 = new Date(1979,3,5);
var dt2 = new Date('1979-04-05');
jsFiddle with example
dt1 is assigned the value: Thu Apr 05 1979 00:00:00 GMT-0500 (Central Daylight Time)
dt2 is assigned the value: Wed Apr 04 1979 19:00:00 GMT-0500 (Central Daylight Time)
Can someone explain this behavior? The second example (dt2) happens to be the format that Chrome is returning a selected date from input[type=date] elements which is why I'm trying to figure this out.
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). The form new Date(1979,3,5); is interpreted as local time. You can use Date.UTC to force UTC time for the 3-argument form (see docs).
Date parsing (and timezone handling in particular) is generally not uniform across browsers, and it's better not to depend on it - use UTC whenever possible, or use a separate library like Date.js or moment.js.
a = new Date();
Sun Apr 08 2012 16:58:03 GMT+0530 (IST)
Is there any way i can get the current UTC time?
I thought of getting an offset doing maths:
b = a.getTimezoneOffset()
-330
then subtract, get the value:
c = a - b
1333884483552
but again getting c as a to look is difficult. So the question:
How can i get the current UTC time, in javascript?
First of all, Date object in JavaScript is timezone-independent. It only stores the number of milliseconds since epoch. Furthermore it always uses browser time zone for toString() and get*() methods. You cannot create Date instance in a different time zone.
Thus simply use getUTC*() family of methods:
new Date().getUTCHours()
new Date().getUTCMinutes()
//...
to obtain time in UTC.
Last but not least - your code is broken. a variable represents Date and is casted to milliseconds here: c = a - b. However b is equal to a.getTimezoneOffset(). The time zone offset is in minutes. You are subtracting minutes from milliseconds...
See also
Annoying javascript timezone adjustment issue
You can use the toUTCString function if you need the string.
new Date().toUTCString()
There is a plugin called jstimezonedetect which you can use to detect the timezone. You can find it here
Or use date's UTC methods like
var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
Demo
Outputs
Date {Sun Apr 08 2012 18:31:50 GMT+0545 (Nepal Standard Time)}
Date {Sun Apr 08 2012 12:46:50 GMT+0545 (Nepal Standard Time)}