简体   繁体   English

我如何在 javascript 中使用 moment.js/Date 将 EST 时间数据转换为本地时间

[英]how I convert EST time data to local time using moment.js / Date in javascript

var res= "03/19/2020 13:15:00"; var res="03/19/2020 13:15:00"; // EST time // 美国东部时间

when i am converting to local timezone当我转换为本地时区时

moment(moment(res.date, 'MM/DD/YYYY HH:mm:ss').utc()).local().format("MM DD YYYY HH:mm:ss")时刻(时刻(res.date, 'MM/DD/YYYY HH:mm:ss').utc()).local().format("MM DD YYYY HH:mm:ss")

I tried many things.我尝试了很多东西。 But I don't know how to convert Est date time to local date time in javascript.但我不知道如何在 javascript 中将 Est 日期时间转换为本地日期时间。

Presumably EST is US Eastern Standard Time, which is GMT+5.据推测,EST 是美国东部标准时间,即 GMT+5。 Most places within that timezone also observe daylight saving, so you may need to determine which set of rules to use.该时区内的大多数地方也遵守夏令时,因此您可能需要确定要使用的规则集。 The offset should be passed with the rest of the timestamp to remove that as a source of error.偏移量与时间戳的其余部分一起传递,以将其作为错误源删除。

Timezone offsets are usually expressed as the time to add to UTC to get the local time, though ECMAScript (and other programming languages) reverse that, ie the time to subtract from UTC to get local.时区偏移量通常表示为添加到 UTC 以获得本地时间的时间,尽管 ECMAScript(和其他编程语言)反转了这一点,即从 UTC 中减去以获得本地时间的时间。

If the offset of the timestamp is known, then you can parse the string as UTC, then apply the offset, then use plain methods to get local values based on host system settings, eg如果时间戳的偏移量已知,那么您可以将字符串解析为 UTC,然后应用偏移量,然后使用普通方法根据主机系统设置获取本地值,例如

 // Offset in minutes, +ve east function parseWithOffset(s, offset) { let b = s.split(/\\D/); let d = new Date(Date.UTC(b[2], b[0]-1, b[1], b[3], b[4], b[5])); d.setUTCMinutes(d.getUTCMinutes() - offset); return d; } let s = '03/19/2020 13:15:00'; // US EST let d = parseWithOffset(s, -300); console.log(d.toString());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在javascript(moment.js)中将UTC日期转换为当地时间 - convert UTC date to local time in javascript (moment.js) 我需要使用 moment.js 根据本地时区转换 UTC 日期和时间,如何将日期从 UTC 转换为 html 中的时区 - I need to convert UTC date and time based on the local timezone using moment.js, how to convert date from UTC to timezone in html 如何使用moment.js和moment-timezone.js将特定时区(非本地)的给定日期/时间转换为UTC - How to convert a given date/time for a specific timezone (not local) to UTC using moment.js and moment-timezone.js 使用 Moment.js 将 UTC 日期和时间转换为本地日期和时间 - Convert UTC Date and Time to local Date and Time using Moment.js 如何使用moment.js将ISO日期/时间转换为本地日期/时间? 接收奇数输出 - How to convert ISO date/time to local date/time with moment.js? Receiving odd output Moment.js:将UTC日期从数据库转换为本地时间 - Moment.js: Convert UTC date from database into local time 使用moment.js处理本地化日期以将其转换为本地日期/时间 - Processing localised date to convert it to local date/time using moment.js 如何使用Moment.js获取今天的EST开始时间和当前时间 - How to get today start time and current time of EST using Moment.js 无法使用 Moment.JS 将字符串日期和时间转换为格式 - Unable to convert the string date and time to format using Moment.JS 在javascript中使用moment.js转换时代时间 - Convert epoch time with moment.js in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM