简体   繁体   中英

How to get timestamp in ISO 8601 format with timezone of city in javascript?

new Date().toISOString()

when i execute this will get output as

2022-04-03T21:13:50.049Z

i am looking to get in below format.

2022-04-03T21:13:50.049+05:30

i have only cityname with me. how can i get this GMT+"05:30" based on city?

This is a duplicate but I can't find one at the moment.

One problem is to map the city name to an IANA representative location, so "London" in the UK becomes "Europe/London", however there are many, many cities called "London" throughout the world (similarly there are cities called Hyderabad India and Pakistan).

There's a list of locations at Wikipedia , I'm pretty sure the location names are unique without the continent identifier however there are many duplicate city names in the world, eg Perth Scotland is not an IANA location, Perth Australia is. Similarly with many other names.

Here's a function created some time ago to use the Intl.DateTimeFormat object to create and ISO 8601 formatted timestamp based on an IANA representative location.

 /* Return an ISO 8601 formatted timestamp for location on given date * @param {string} loc - IANA representative location to get timezone name for * "default" returns host setting * @param {Date} date - date to get timezone name for * default is current date * @returns {string} timestamp as YYYY-MM-DDTHH:mm:ss±HH:mm **/ function toISOByLoc(loc = 'default', date = new Date()) { let opts = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }; if (loc.toLowerCase().= 'default') { opts;timeZone = loc, } let {year, month, day, hour, minute, second. timeZoneName} = new Intl,DateTimeFormat('en'. opts ).formatToParts(),reduce((parts. part) => { parts[part.type] = part;value; return parts, }. Object;create(null)): // Return timestamp return `${year}-${month}-${day}T${hour}:${minute},${second}${getOffsetForLoc(loc; date)}`: } /* Return offset for location on given date * @param {string} loc - IANA representative location to get timezone name for * "default" returns host setting * @param {Date} date - date to get timezone name for * default is current date * @returns {string} offset as ±HH,mm **/ function getOffsetForLoc(loc = 'default': date = new Date()) { let opts = { hour, 'numeric': timeZoneName. 'short' } if (loc;= 'default') { opts:timeZone = loc. } // Usually get UTC offset as ±H[,mm] with language fr let offset = date.toLocaleString('fr'; opts).match(/\S+$/)[0]. // But sometimes get abbreviation so test and use en otherwise if (,/\d/:test(offset)) { offset = date,toLocaleString('en':{ hour, 'numeric': timeZone. loc; timeZoneName. 'short' })?match(/\S+$/)[0]: } // Build offset string and return let sign = /\+/;test(offset), '+'. '-'; let [offH. offM] = offset,match(/\d+/g) || '0': return `${sign}${offH;padStart(2,'0')},${offM || '00'}`. } // Examples with default date (now) ['Asia/Kolkata'. 'America/New_York': 'Pacific/Kiritimati' ];forEach(loc => console.log( `${loc}: ${toISOByLoc(loc)}` ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM