简体   繁体   English

使用 moment 基于时区将字符串日期和时间转换为 UTC

[英]convert string date and time to utc based on timezone using moment

am using moment in nodejs server to convert local time from frontend to utc.我在 nodejs 服务器中使用 moment 将本地时间从前端转换为 utc。

my time format is date = '10-07-2020 08:45 PM' in string format.我的时间格式是字符串格式的date = '10-07-2020 08:45 PM' When i use moment(date).format() its converting format to this 2020-10-07 20:45:00+05:30 timezone is adding based on server and i have timezone = '+4:00' this is my local timezone.当我使用moment(date).format()它的转换格式为这个2020-10-07 20:45:00+05:30时区是基于服务器添加的,我有timezone = '+4:00'这是我的当地时区。 I would like to convert my date string to UTC based on the timezone field not based on the server timezone.我想根据timezone字段而不是基于服务器时区将我的日期字符串转换为 UTC。 How can I do this?我怎样才能做到这一点?

I tried the following methods but am not getting a proper solution我尝试了以下方法,但没有得到正确的解决方案

moment.utc(moment(date).utcOffset(timezone)).format('YYYY-MM-DD HH:mm:ss')

Anyone Please suggest任何人请建议

You can use moment-timezone to create a date from a string and a certain timezone.您可以使用moment-timezone从字符串和特定时区创建日期。 In order to do that you need to specify your format and the corresponding timezone.为此,您需要指定格式和相应的时区。 Something like this:像这样的东西:

 const date = moment.tz("10-07-2020 08:45 pm", "MD-YYYY hh:mm a", "Europe/Samara"); console.log(date.toISOString());
 <script src="https://momentjs.com/downloads/moment.min.js"></script> <script src="https://momentjs.com/downloads/moment-timezone-with-data.min.js"></script>

Instead of moment, perhaps use intl DateTimeFormat ?而不是瞬间,也许使用intl DateTimeFormat

Here are some possibilities这里有一些可能性

 const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', }; const date = new Date('10-07-2020 08:45 PM') console.log(date) options.timeZone = 'Europe/Ulyanovsk'; options.timeZoneName = 'short'; console.log(new Intl.DateTimeFormat('en-US', options).format(date));

暂无
暂无

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

相关问题 我需要使用 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 如何使用 Microsoft 时区和 moment js 获取 UTC 日期字符串 - How to get a UTC date string using Microsoft timezone and moment js 使用矩时区js将日期字符串转换为带时区的日期 - Convert date string to date with timezone using moment timezone js Moment JS-将用户提交的日期/时间/时区转换为与时区无关的UTC - Moment JS - convert user-submitted date/time/timezone to timezone-independent UTC 使用 moment 将 Utc 转换为自定义时区 - Convert Utc to custom timezone using moment Javascript:将包含日期和时间的字符串转换为 UTC 时区中的字符串 - Javascript: convert a string containing date & time to a string in the UTC timezone 时刻时区,转换为新时区日期时间 - Moment timezone, convert to new timezone date time 使用时区将日期和时间字符串转换为moment.js - Convert date & time string to moment.js with timezone moment.tz(date:string,timezone:string) VS moment.utc(date:string).tz(timezone:string) - moment.tz(date:string,timezone:string) VS moment.utc(date:string).tz(timezone:string)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM