Date.toISOString() but local time instead of UTC

Asked
Active3 hr before
Viewed126 times

7 Answers

local
90%

There is limited built-in support for formatting date strings with timezones in ECMA-262, there is either implementation dependent toString and toLocaleString methods or toISOString, which is always UTC. It would be good if toISOString allowed a parameter to specify UTC or local offset (where the default is UTC).,Making statements based on opinion; back them up with references or personal experience.,The natural idea then would be to ask for an ISO8601 datetime, and use d.toISOString(); but it gives the UTC datetime: 2018-07-21T12:00:00.000Z whereas I would like the local-timezone time instead:, Quitting before the start date: is the non compete enforceable?

function toISOLocal(d) {
   var z = n => ('0' + n).slice(-2);
   var zz = n => ('00' + n).slice(-3);
   var off = d.getTimezoneOffset();
   var sign = off < 0 ? '+' : '-';
   off = Math.abs(off);

   return d.getFullYear() + '-' +
      z(d.getMonth() + 1) + '-' +
      z(d.getDate()) + 'T' +
      z(d.getHours()) + ':' +
      z(d.getMinutes()) + ':' +
      z(d.getSeconds()) + '.' +
      zz(d.getMilliseconds()) +
      sign + z(off / 60 | 0) + ':' + z(off % 60);
}

console.log(toISOLocal(new Date()));
load more v
88%

ISO-8601 datetime strings are of the forms :,Javascript has a toISOString method that gives a datetime in ISO-8601 format. But it gives datetime strings only in UTC format, YYYY-MM-DDTHH:mm:ss.sssZ. It does not give a timezone offset in hours and minutes. To get this, you need a bit of custom code.,Concatenate datetime and timezone offset to get an ISO-8601 datetime string.,If you want a datetime string set to some other date & time (not the current time), pass an appopriate parameter to the Date object. See Several ways to create a Date object for more.

Note that getTimezoneOffset returns an offset which is positive if the local timezone is behind UTC and negative if it is ahead. So we must add an opposite sign (+ or -) to the offset.

var timezone_offset_min = new Date().getTimezoneOffset(),
   offset_hrs = parseInt(Math.abs(timezone_offset_min / 60)),
   offset_min = Math.abs(timezone_offset_min % 60),
   timezone_standard;

if (offset_hrs < 10)
   offset_hrs = '0' + offset_hrs;

if (offset_min < 10)
   offset_min = '0' + offset_min;

// Add an opposite sign to the offset
// If offset is 0, it means timezone is UTC
if (timezone_offset_min < 0)
   timezone_standard = '+' + offset_hrs + ':' + offset_min;
else if (timezone_offset_min > 0)
   timezone_standard = '-' + offset_hrs + ':' + offset_min;
else if (timezone_offset_min == 0)
   timezone_standard = 'Z';

// Timezone difference in hours and minutes
// String such as +5:30 or -6:00 or Z
console.log(timezone_standard);
load more v
72%

Let's say we have this datetime:,Sat Jul 21 2018 14:00:00 GMT+0200 (Paris, Madrid (heure d’été)) with Chrome, Convert UTC to local time in go ,Sat Jul 21 14:00:00 UTC+0200 2018 with Internet Explorer, etc.

Let's say we have this datetime:

var d = new Date("Sat Jul 21 2018 14:00:00 GMT+0200");

The natural idea then would be to ask for an ISO8601 datetime, and use d.toISOString(); but it gives the UTC datetime: 2018-07-21T12:00:00.000Z whereas I would like the local-timezone time instead:

2018 - 07 - 21 T14: 00: 00 + 0200
or
2018 - 07 - 21 T14: 00: 00
var pad = function(i) {
   return (i < 10) ? '0' + i : i;
};

var d = new Date("Sat Jul 21 2018 14:00:00 GMT+0200");
Y = d.getFullYear();
m = d.getMonth() + 1;
D = d.getDate();
H = d.getHours();
M = d.getMinutes();
S = d.getSeconds();
s = Y + '-' + pad(m) + '-' + pad(D) + 'T' + pad(H) + ':' + pad(M) + ':' + pad(S);
console.log(s);
load more v
65%

Program 1: Here nothing as a parameter is passed while creating date object but still toISOString() method return current day name, month name, date, year, and time.,Program 2: Here we will pass a date object toISOString() method return day name, month name, date, year, and time.,Parameters: This method does not take any parameter. It is just used along with a Date object created using Date() constructor.,The date.toISOString() method is used to convert the given date object’s contents into a string in ISO format (ISO 8601) i.e, in the form of (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ).The date object is created using date() constructor.

load more v
75%

Date.UTC() uses universal time instead of the local time., The Date.UTC() method accepts parameters similar to the Date constructor, but treats them as UTC. It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. , The UTC() method differs from the Date constructor in two ways: , Date.UTC() returns a time value as a number instead of creating a Date object.

Date.UTC(year)
Date.UTC(year, month)
Date.UTC(year, month, day)
Date.UTC(year, month, day, hour)
Date.UTC(year, month, day, hour, minute)
Date.UTC(year, month, day, hour, minute, second)
Date.UTC(year, month, day, hour, minute, second, millisecond)
load more v
40%

Let’s start with a brief overview of what it means for something to happen in the universe.,Take a deep breath and think through the following principles:,But, of course, it isn’t. We do not always want to, and do not always have to, think about dates as occurring at a moment in time.,Not every date is really something that happened at a particular moment in time.

It happened at a particular moment in time, and we mostly want to know what time this was in our local timezone. JavaScript is excellent at dealing with this sort of representation of a date, whether we’re working on the browser or the server.

> new Date("2020-01-08T19:47:00.000Z")
Wed Jan 08 2020 14: 47: 00 GMT - 0500(Eastern Standard Time)

   >
   moment("2020-01-08T19:47:00.000Z").format("h:mm a MMM DD, YYYY") // using moment.js
"2:47 pm Jan 08, 2020"

>
format(parseISO("2020-01-08T19:47:00.000Z"), "h:mm a MMM dd, yyyy") // using date-fns
"2:47 PM Jan 08, 2020"
load more v
22%

var isoDate = new Date().toISOString()
load more v

Other "local-undefined" queries related to "Date.toISOString() but local time instead of UTC"