DayJS: 将 ISO 日期/时间字符串格式化并转换为本地时区的日期/时间

huangapple go评论89阅读模式
英文:

DayJS: Format and Convert an ISO Date/Time String to Local Timezone's Date/Time

问题

我正在使用一个返回时间戳的API,时间戳的格式是 2023-02-18T14:54:28.555Z,这是一个ISO字符串。我需要将这个值格式化为用户所在时区的时间。

我尝试了以下方法:
dayjs("2023-02-18T14:54:28.555Z").format('YYYY-MM-DD HH:MM:ss A') // => "2023-02-18 20:02:28 PM"

上述输出是不正确的,与+0530 IST时区相比,它慢了30分钟。

但是当我将相同的字符串 "2023-02-18T14:54:28.555Z" 传递给JavaScript日期构造函数时,我可以看到正确的值。
new Date("2023-02-18T14:54:28.555Z").toString() // => 'Sat Feb 18 2023 20:24:28 GMT+0530 (印度标准时间)'

如何使用DayJS获取正确的时区格式化值?

尝试将ISO字符串传递给DayJS构造函数,期望它会解析为当前时区。但输出值比实际时间慢了30分钟。

英文:

I'm consuming an API which returns timestamps in this format 2023-02-18T14:54:28.555Z which is an ISO string. I need to format this value to the timezone of the user.

I've tried this:
dayjs("2023-02-18T14:54:28.555Z").format('YYYY-MM-DD HH:MM:ss A') // => "2023-02-18 20:02:28 PM"

The above output is incorrect and is 30 minutes behind for +0530 IST Timezone.

But when I input the same string "2023-02-18T14:54:28.555Z" to the JavaScript date constructor, I can see the correct value.
new Date("2023-02-18T14:54:28.555Z").toString() // => 'Sat Feb 18 2023 20:24:28 GMT+0530 (India Standard Time)'

How to get the correct formatted value for my Timezone using DayJS?

Tried feeding the ISO string to the DayJS constructor and expected it'll parse it to the current timezone. But the output value is 30 minutes behind.

答案1

得分: 2

Date.toString() 会根据操作系统的本地时间显示日期。如果您需要在除操作系统本地时间之外的时区中显示时间,那么您需要使用 DayJS Timezone 插件。

const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');

const timestamp = '2023-02-18T14:54:28.555Z';

dayjs.extend(utc);
dayjs.extend(timezone);

// 由于我的操作系统设置为 America/Los_Angeles 时区,因此显示西雅图时间。
const seattleString = Date(timestamp).toString();

const dayjsLocal = dayjs(timestamp);
const dayjsIst = dayjsLocal.tz('Asia/Calcutta');
const istString = dayjsIst.format('YYYY-MM-DDTHH:mm:ss');

console.log(seattleString); // 星期日,2023年2月19日 02:43:42 GMT-0800(太平洋标准时间)

console.log(istString); // 2023-02-18T20:24:28
英文:

Date.toString() displays the Date according to the local time of the OS. If you need the time to display in a zone other than the local time of the OS, then you'll have to use the DayJS Timezone plugin.

const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');

const timestamp = '2023-02-18T14:54:28.555Z';

dayjs.extend(utc);
dayjs.extend(timezone);

// Seattle time because my OS is set to America/Los_Angeles time.
const seattleString = Date(timestamp).toString();

const dayjsLocal = dayjs(timestamp);
const dayjsIst = dayjsLocal.tz('Asia/Calcutta');
const istString = dayjsIst.format('YYYY-MM-DDTHH:mm:ss');

console.log(seattleString); // Sun Feb 19 2023 02:43:42 GMT-0800 (Pacific Standard Time)

console.log(istString); // 2023-02-18T20:24:28

答案2

得分: 0

你可以使用toLocaleString()方法:

const timestamp = "2023-02-18T14:54:28.555Z";
const date = new Date(timestamp);

const options = { timeZone: 'Asia/Kolkata' };
const formattedDate = date.toLocaleString('en-US', options);

console.log(formattedDate);

这段代码用于将时间戳转换为特定时区的本地时间字符串。

英文:

you can use toLocaleString() method:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const timestamp = &quot;2023-02-18T14:54:28.555Z&quot;;
const date = new Date(timestamp);


const options = { timeZone: &#39;Asia/Kolkata&#39; };
const formattedDate = date.toLocaleString(&#39;en-US&#39;, options);

console.log(formattedDate); 

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 17:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定