[英]Moment.js Convert Local time to UTC time does work
I would like to use Moment.js to convert a local time to UTC equivalent.我想使用 Moment.js 将本地时间转换为 UTC 等效时间。 I believe that I have the correct method in place, but it does not alter the time.我相信我有正确的方法,但它不会改变时间。
I'm in Sydney Australian +11 and expect the UTC time to be 11 hours earlier.我在澳大利亚悉尼 +11,希望 UTC 时间早 11 小时。
Internally on the moment object the isUTC flag changes from false to true, but the time does NOT shift, am I meant to use a different technique for this.在 object 时刻内部,isUTC 标志从 false 变为 true,但时间没有改变,我是否打算为此使用不同的技术。
How do I actually get the current UTC date out of this object我如何从这个 object 中实际获取当前的 UTC 日期
Before Conversion转换前
var val = '18/03/2015';
var selectedDate = moment(val, 'DD/MM/YYYY');
After Conversion转换后
var a = selectedDate.utc()
I just tried this code and it seems like I get the correct UTC time.我刚刚试过这段代码,似乎我得到了正确的 UTC 时间。 I guess I just want to confirm that what I am doing is correct way to access the UTC time from moment.js我想我只是想确认我正在做的是从 moment.js 访问 UTC 时间的正确方法
a.format("YYYY-MM-DD HH:mm:ssZ")
I found that my usage pattern of in my application was incorrect我发现我在我的应用程序中的使用模式不正确
selectedDate.utc().format(fullFormat)
It should have been应该是
moment.utc(selectedDate).format(fullFormat)
这有效
moment(date_to_convert).utc().format("YYYY-MM-DD HH:mm:ss");
The question is old, but I also faced it.这个问题很老,但我也遇到过。 It may be useful to someone:它可能对某人有用:
Using the method of utcOffset() to calculate the UTC time:使用utcOffset()的方法计算UTC时间:
selectedDate = (moment(selectedDate).add(-(moment().utcOffset()), 'm'));
And explicitly specify UTC:并明确指定UTC:
selectedDate = moment.parseZone(selectedDate).utc().format();
这对我有用!!
selectedDate = moment(selectedDate).add(moment(selectedDate).utcOffset(), 'm').utc().format()
Create a local moment object from you local time and convert it to UTC then format it, then create a new UTC moment from that formatted UTC string从您的本地时间创建一个本地时刻对象并将其转换为 UTC 然后对其进行格式化,然后从该格式化的 UTC 字符串中创建一个新的 UTC 时刻
var localDateString = '24/04/2019'; var localDateStringFormat = 'DD/MM/YYYY'; var utcMoment = moment.utc(moment(localDateString, localDateStringFormat ).utc().format('YYYY-MM-DD HH:mm:ssZ')) console.log(utcMoment);
<script src="https://momentjs.com/downloads/moment.js"></script>
After few frustrating hours, I found what was the problem经过几个令人沮丧的小时后,我发现了问题所在
Short Answer: To convert time to utc, we need to use format()
简短回答:要将时间转换为 UTC,我们需要使用format()
Long Answer: Take the example长答案:举个例子
moment.utc(1559586600000).format('LLL')
.utc sets the isUTC flag to true. .utc 将 isUTC 标志设置为 true。
When logging the date, the d
key always shows the time in local timezone.记录日期时, d
键始终显示本地时区的时间。 (Which makes us believe its not working properly - as shown in your screenshot) (这让我们相信它无法正常工作 - 如您的屏幕截图所示)
But we need to use .format to get the date/time in UTC format.但是我们需要使用 .format 来获取 UTC 格式的日期/时间。
The above code returns June 3, 2019 6:30 PM
which is the correct UTC time.上面的代码返回June 3, 2019 6:30 PM
,这是正确的 UTC 时间。
这就是您使用moment-timezone
moment.tz(localDate, localTimeZone).utc()
const moment = require('moment-timezone');
const dateTime='2020-12-21'
const timezone='America/Anchorage'
const dateTimeInUtc = moment(dateTime).tz(timezone).utc().format();
console.log('dateTimeInUtc',dateTimeInUtc);
After few frustrating hours, I found what was the problem经过几个令人沮丧的小时后,我发现了问题所在
Short Answer: To convert time to utc, we need to use format()简短回答:要将时间转换为 utc,我们需要使用 format()
Long Answer: Take the example长答案:举个例子
moment.utc(1559586600000).format('LLL') moment.utc(1559586600000).format('LLL')
.utc sets the isUTC flag to true. .utc 将 isUTC 标志设置为 true。
When logging the date, the d key always shows the time in local timezone.记录日期时,d 键始终显示本地时区的时间。 (Which makes us believe its not working properly - as shown in your screenshot) (这让我们相信它无法正常工作 - 如您的屏幕截图所示)
But we need to use.format to get the date/time in UTC format.但是我们需要使用 .format 来获取 UTC 格式的日期/时间。
The above code returns June 3, 2019 6:30 PM which is the correct UTC time.上面的代码返回 2019 年 6 月 3 日下午 6:30,这是正确的 UTC 时间。
const moment = require('moment-timezone'); const dateTime='2020-12-21' const timezone='America/Anchorage' const dateTimeInUtc = moment(dateTime).tz(timezone).utc().format(); console.log('dateTimeInUtc',dateTimeInUtc);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.