Why New Date().Toisostring() Is Loosing Timezone

javascript toISOString() ignores timezone offset

moment.js is great but sometimes you don't want to pull a large number of dependencies for simple things.

The following works as well:

    var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -1);

console.log(localISOTime) // => '2015-01-26T06:40:36.181'

offset time zone problem with toIsoString : does not convert well the date object

The date produced by new Date() makes an allowance for the host timezone offset, the toISOString method is UTC so unless the host is set to UTC, there will be a difference equal to the host timezone offset. In the OP that's +01:00.

If you want the start of the year UTC, then you have to first generate a time value as UTC, then use that for the date:

// Get the current UTC yearlet year = new Date().getUTCFullYear();// Create a Date avoiding the local offsetlet d = new Date(Date.UTC(year, 0, 1));// Get a UTC timestampconsole.log(d.toISOString());

JavaScript Date.prototype.toISOString() loses offset

This is one of those "because that's what the language specification says" answers (see ECMA-262 §20.3.4.36). ISO 8601 is a format, and while it allows the use of timezone data, ECMAScript only uses UTC. You can extend Date.prototype with your own toLocalISOString method if you wish. BTW, writing such a method is not difficult.

// Format date as ISO 8601 long format with local timezone offsetif (!Date.prototype.toLocalISOString) {  Date.prototype.toLocalISOString = function() {    // Helper for padding  function pad(n, len) {    return ('000' + n).slice(-len);  }
// If not called on a Date instance, or timevalue is NaN, return undefined if (isNaN(this) || Object.prototype.toString.call(this) != '[object Date]') return;
// Otherwise, return an ISO format string with the current system timezone offset var d = this; var os = d.getTimezoneOffset(); var sign = (os > 0? '-' : '+'); os = Math.abs(os);
return pad(d.getFullYear(), 4) + '-' + pad(d.getMonth() + 1, 2) + '-' + pad(d.getDate(), 2) + 'T' + pad(d.getHours(), 2) + ':' + pad(d.getMinutes(), 2) + ':' + pad(d.getSeconds(), 2) + '.' + pad(d.getMilliseconds(), 3) + // Note sign of ECMASCript offsets are opposite to ISO 8601 sign + pad(os/60 | 0, 2) + ':' + pad(os%60, 2); }}document.write(new Date().toLocalISOString())

Is the Javascript date object always one day off?

Notice that Eastern Daylight Time is -4 hours and that the hours on the date you're getting back are 20.

20h + 4h = 24h

which is midnight of 2011-09-24. The date was parsed in UTC (GMT) because you provided a date-only string without any time zone indicator. If you had given a date/time string w/o an indicator instead (new Date("2011-09-24T00:00:00")), it would have been parsed in your local timezone. (Historically there have been inconsistencies there, not least because the spec changed more than once, but modern browsers should be okay; or you can always include a timezone indicator.)

You're getting the right date, you just never specified the correct time zone.

If you need to access the date values, you can use getUTCDate() or any of the other getUTC*() functions: