0

When i try

new Date().toISOString()

I have the following timestamp output

2021-03-25T11:05:10.140Z

I've read somewhere that we can get timezone from the utc or unix timestamp.

But they were not explaining how we can do that.Is this possible ?

pece
  • 87
  • 9
  • 2
    You cannot. A `Date` object, and a "UTC or Unix timestamp", does not contain that information. It only contains a number of (milli)seconds since 01-01-1970, 00:00:00 UTC. There is no information about a timezone in that. – Jesper Mar 25 '21 at 11:13
  • 1
    [*getTimezoneOffset*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset) returns an offset that is minutes to add to the local time to get UTC (so -ve east and +ve west of Greenwich). The value is based on the host system's settings, it's not a property of Date instances. [*toISOString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) is defined as returning UTC. – RobG Mar 25 '21 at 11:18
  • @RobG can you please write an answer for your example with the getTimezoneOffset ? Thanks in advance – pece Mar 25 '21 at 11:21
  • To convert minutes to HH:mm see [*How to convert minutes to time(hh:mm:ss)?*](https://stackoverflow.com/questions/41391403/how-to-convert-minutes-to-timehhmmss) – RobG Mar 25 '21 at 11:25
  • Are you looking for a time zone *identifier* (such as `America/Los_Angeles`)? Or a time zone *offset* (such as `-08:00`)? They are not the same thing. See "Time Zone != Offset" in [the timezone tag wiki](https://stackoverflow.com/tags/timezone/info). – Matt Johnson-Pint Mar 25 '21 at 18:38
  • time zone identifier i am looking for – pece Mar 26 '21 at 05:59

1 Answers1

0

You can get the host timezone offset for the date using getTimezoneOffset which is minutes to add to the local date and time to UTC (or alternatively, the minutes to subtract from UTC to get local).

Therefore the sign is +ve for west of and -ve for east, which is the opposite of commonly used offsets. The offset in minutes can be converted to +HH:mm using a simple function that reverses the sign and formats the value.

Most offsets since 1900 are even multiples of 15 minutes, prior to that offsets were based on local solar noon so included seconds. So any function that returns an offset as ±HH:mm must include seconds if they are present in the offset.

Since the offset is in minutes, seconds (if present) are represented as decimal minutes. Since 60ths can't be represented exactly by ECMAScript numbers, the minor decimal places need to be dealt with.

E.g. 1 Jan 1800 in Australia/Sydney was +10:04:52 which is represented as -604.8666666666667 minutes. That converts simplistically to +10:04:52.000000000000455, so the decimal seconds need to be rounded to the nearest whole number to get +10:04:52.

/* Get host timezone offset for date
** @param {Date} date : date to get timezone offset for
** @returns {string|undefined} offset as "±HH:mm[:ss]" or
**          undefined if date is invalid
**          seconds only included if not zero
*/ 
function getOffset(date = new Date()) {
  let z = n => (n < 10? '0' : '') + n;
  let m = date.getTimezoneOffset();
  let sign = m < 0? '+' : '-';
  m = Math.abs(m);
  let HH = z(m / 60 | 0);
  let mm = z(m % 60 | 0);
  let ss = z(Math.round((m % 1) * 60));
  return isNaN(m)? void 0 : `${sign}${HH}:${mm}${ss=='00'?'':':'+ss}`;
}
 
[new Date(1800,0,1), // 1 Jan 1800, offset may have minutes
 new Date(2020,0,1), // 1 Jan 2020, may be DST in southern hemisphere
 new Date(2020,5,1), // 1 Jun 2020, may be DST in northern hemisphere
 new Date(NaN)       // invalid date
].forEach(d => console.log(d.toDateString() + ' ' + getOffset(d)));
RobG
  • 142,382
  • 31
  • 172
  • 209