I need a js Date object with specified values for date and year. I would expect
new Date("2000-01-01")
to give me Date object with 2000 as value for getFullYear()
, but if my computer's time settings are set to Chicago timezone, I'm getting Fri Dec 31 1999 18:00:00 GMT-0600 (CST)
, and for Buenos Aires: Fri Dec 31 1999 22:00:00 GMT-0200 (ARST)
.
Is there a way to create Date object, with .getFullYear()
returning the date we set in constructor, no matter what timezone is set on user's machine?
Update:
I need this Date object to be used in another library (which calls its .getFullYear()
method, so using UTC getters doesn't really help.
To do this, go to Settings > Privacy > Location Services > System Services and turn on Setting Time Zone. Check that your device shows the correct time zone in Settings > General > Date & Time > Time Zone.
The date object itself (as shown by your link) is the number of milliseconds since 1 January 1970 UTC - so therefore the Date itself doesn't have a timezone - it's just a number. It's only when you display it with toString that you see YOUR timezone.
Yes, Date. now() will give you the same UTC timestamp independent of your current timezone. Such a timestamp, rather a point in time, does not depend on timezones. The Java equivalent new Date() gives you the exact same thing.
The JavaScript Date is always stored as UTC, and most of the native methods automatically localize the result.
When parsing a string to a Date
in JavaScript, a value that is in YYYY-MM-DD
format is interpreted as a UTC value, rather than a local-time value.
The key is that the parts are separated by hyphens, and that there is no time zone information in the string. The ECMAScript 5.1 Spec says in §15.9.1.15:
... The value of an absent time zone offset is “Z”.
That means, if you don't specify an offset, it will assume you meant UTC.
Note that since this is the opposite of what ISO-8601 says, this is behavior has been changed in ECMAScript 2015 (6.0), which says in §20.3.1.16:
... If the time zone offset is absent, the date-time is interpreted as a local time.
Therefore, when this provision of ES6 is implemented properly, string values of this format that used to be interpreted as UTC will be interpreted as local time instead. I've blogged about this here.
The workaround is simple. Replace the hyphens with slashes:
var s = "2000-01-01";
var dt = new Date(s.replace(/-/g, '/'));
Another workaround that is acceptable is to assign a time of noon instead of midnight to the date. This will be parsed as local time, and is far enough away to avoid any DST conflicts.
var s = "2000-01-01";
var dt = new Date(s + "T12:00:00");
Alternatively, consider a library like moment.js which is much more sensible.
var s = "2000-01-01";
var dt = moment(s, 'YYYY-MM-DD').toDate();
You can write new method to 'Date.prototype', and use it to get date which will be including the local timezone offset.
//return the date with depend of Local Time Zone
Date.prototype.getUTCLocalDate = function () {
var target = new Date(this.valueOf());
var offset = target.getTimezoneOffset();
var Y = target.getUTCFullYear();
var M = target.getUTCMonth();
var D = target.getUTCDate();
var h = target.getUTCHours();
var m = target.getUTCMinutes();
var s = target.getUTCSeconds();
return new Date(Date.UTC(Y, M, D, h, m + offset, s));
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With