Home > Back-end >  Initializing time with its own timezone
Initializing time with its own timezone

Time:08-13

I'm quite new (and confused) with time in JavaScript..

I currently have time data to work with, and they are in the format of DD-MMM-YYYY, meaning it would be 23-Feb-2021. This time is already in its own timezone, GMT-10. I'm trying to initialize it as GMT-10 so that I could get its appropriate epoch time. I've done this:

date = new Date("23-Feb-2021") // This results in 2021-02-23T00:00:00.000Z

But what I'm trying to achieve is to get the time to be 2021-02-23T10:00:00.000Z, which I could then do a getTime() to get its epoch in ms. I understand I could probably hard code to 10 to the time I have, but the data I work with might vary so I'd like to figure a way to initialize the date with a specific timezone.

EDIT: Here's an example of an outcome I'd want:

date = ("23-Feb-2021")

date = moment(date).format(); // 2021-02-23T00:00:00 00:00
date = date.replace(" 00"," 10");
date = new Date(msg.date); // 2021-02-22T14:00:00.000Z
date = date.getTime(); // 1614002400000 (2021-02-22T14:00:00.000Z)

In the end, 2021-02-22T14:00:00.000Z is what I'm trying to get, without having to iterate it a bunch of times like above and adding 10

CodePudding user response:

You can add the timezone offset to your input string, and use an explicit string format to parse it:

let date = "23-Feb-2021"
date = moment(date   "-10:00", "D-MMM-YYYYZ")
console.log(date.format()) // 2021-02-23T11:00:00 01:00 (if local is GMT 1)
console.log(date.utc().format()) // 2021-02-23T10:00:00 00:00

CodePudding user response:

It seems you're already using moment.js, so add moment–timezone so you can parse timestamps in whatever IANA timezone you want. You can either choose a location with the offset rules you want (e.g. Pacific/Honolulu or Pacific/Tahiti for -10) or just a fixed offset like etc/GMT 10.

You can then format the value in any timezone, as UTC, or as a time value, e.g.

// Timestamp
let d = "23-Feb-2021";

// Parse in specific IANA timezone
let m = moment.tz(d, 'D-MMM-YYYY', 'Pacific/Honolulu');

// Trigger UTC mode
m.utc()

// Show result
console.log(m.format())

// Get time value (ms since epoch)
console.log(m.valueOf());

// Parse using generic timezone/fixed offset
let g = moment.tz(d, 'D-MMM-YYYY', 'etc/GMT 10');
console.log(g.utc().format());

// Display timestamp for another timezone
console.log(moment.tz(g, 'Asia/Riyadh').format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone-with-data-10-year-range.js"></script>

Note that for fixed offset timezones like etc/GMT 10, the sign is the opposite of the common offset (e.g. etc/GMT 10 is UTC-10) to be consistent with POSIX notation. However, moment.tz only has limited POSIX support in that it only recognises one hour offsets, not the full POSIX timezone notation, so you can't do say "etc/GMT-530" instead of Asia/Kolkata.

  • Related