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'
The slice(0, -1) gets rid of the trailing Z which represents Zulu timezone and can be replaced by your own.
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'
The slice(0, -1) gets rid of the trailing Z which represents Zulu timezone and can be replaced by your own.
My solution without using moment is to convert it to a timestamp, add the timezone offset, then convert back to a date object, and then run the toISOString()
var date = new Date(); // Or the date you'd like converted.
var isoDateTime = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString();
It's very unclear what you're asking. If you want the UTC date with the hours always 0, then set the UTC hours to 0 and use toISOString, e.g.
var d = new Date();
d.setUTCHours(0,0,0,0);
console.log(d.toISOString());
Of course this is going to show the UTC date, which may be different to the date on the system that generated the Date.
Also,
new Date('2017-04-27').toISOString();
should return 2017-04-27T00:00:00Z (i.e. it should be parsed as UTC according to ECMA-262, which is contrary to ISO 8601 which would treat it as local), however that is not reliable in all implementations in use.
If you just want to get the current date in ISO 8601 format, you can do:
if (!Date.prototype.toISODate) {
Date.prototype.toISODate = function() {
return this.getFullYear() + '-' +
('0'+ (this.getMonth()+1)).slice(-2) + '-' +
('0'+ this.getDate()).slice(-2);
}
}
console.log(new Date().toISODate());
However, since the built-in toISOString uses UTC this might be confusing. If the UTC date is required, then:
if (!Date.prototype.toUTCDate) {
Date.prototype.toUTCDate = function() {
return this.getUTCFullYear() + '-' +
('0'+ (this.getUTCMonth()+1)).slice(-2) + '-' +
('0'+ this.getUTCDate()).slice(-2);
}
}
console.log(new Date().toUTCDate());
old_date = Fri Jan 08 2021 16:01:30 GMT+0900
const new_date = old_date.toISOString().substring(0, 10);
new_date = "2021-01-08"
Based on the polyfill for Date.prototype.toISOString found at MDN's Date.prototye.toISOString:
if (!Date.prototype.toLocalISOString) {
(function() {
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
Date.prototype.toLocalISOString = function() {
return this.getFullYear() +
'-' + pad(this.getMonth() + 1) +
'-' + pad(this.getDate()) +
'T' + pad(this.getHours()) +
':' + pad(this.getMinutes()) +
':' + pad(this.getSeconds()) +
'.' + (this.getMilliseconds() / 1000).toFixed(3).slice(2, 5) +
'Z';
};
}());
}
So just use this toLocalISOString instead of toISOString.
Your Javascript statements are producing the expected results. Tue May 26 2015 14:00:00 GMT+0100 is the same time as 2015-05-26T13:00:00.000Z. Unless you care about a fraction of a second, GMT, UTC, and Z all mean the same thing: mean solar time at 0° longitude, and keeping the same time all year (no change in summer; Iceland observes this time all year). "GMT+0100" means a time zone that is one hour later than 0° longitude, such the United Kingdom in summer. So at a given moment the time in Iceland is 2015-05-26T13:00:00.000Z and also Tue May 26 2015 14:00:00 GMT+0100 in London. Could you clarify what result you want to see? Perhaps you would like "2015-05-26T14:00:00" which is the ISO 8601 notation for some unspecified local time, presumably the local time of the computer displaying the Javascript. Or maybe you want "2015-05-26T14:00:00+01:00", which is ISO 8601 notation for the local time zone one hour ahead of 0° longitude, such as London in summer or Paris in winter.
Building on souldreamer's example, making it fully working and providing the local time offset:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<pre>
<script language="JavaScript">
if (!Date.prototype.toLocalISOString) {
// Anonymous self-invoking function
(function () {
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
Date.prototype.toLocalISOString = function () {
timestamp = this.getFullYear() +
'-' + pad(this.getMonth() + 1) +
'-' + pad(this.getDate()) +
'T' + pad(this.getHours()) +
':' + pad(this.getMinutes()) +
':' + pad(this.getSeconds());
if (this.getTimezoneOffset() == 0) { timestamp = timestamp + "Z" }
else {
if (this.getTimezoneOffset() < 0) { timestamp = timestamp + "+" }
else { timestamp = timestamp + "-" }
timestamp = timestamp + pad(Math.abs(this.getTimezoneOffset() / 60).toFixed(0));
timestamp = timestamp + ":" + pad(Math.abs(this.getTimezoneOffset() % 60).toFixed(0));
}
return timestamp;
};
}());
}
now = new Date();
document.writeln("Create date object containing present time and print UTC version.");
document.writeln(now.toUTCString());
document.writeln("Show local time version");
document.writeln(now.toLocalISOString());
</script>
</pre>