繁体   English   中英

使用 moment.js 将日期转换为 UTC

[英]Convert date to UTC using moment.js

可能很容易回答这个问题,但我似乎无法找到让 moment.js 以毫秒为单位返回 UTC 日期时间的方法。 这是我正在做的事情:

var date = $("#txt-date").val(),
    expires = moment.utc(date);

知道我做错了什么吗?

这可以在文档中找到。 对于像图书馆这样的时刻,我敦促您阅读整个文档。 真的很重要。

假设输入文本是根据用户的本地时间输入的:

 var expires = moment(date).valueOf();

如果指示用户实际输入 UTC 日期/时间,则:

 var expires = moment.utc(date).valueOf();

我使用这种方法并且它有效。 ValueOf 对我不起作用。

moment.utc(yourDate).format()

截至: moment.js version 2.24.0

假设您有本地日期输入,这是dateTimeTime输入转换为UTC的正确方法:

var utcStart = new moment("09:00", "HH:mm").utc();

或者如果您指定日期

var utcStart = new moment("2019-06-24T09:00", "YYYY-MM-DDTHH:mm").utc();

如您所见,结果输出将以 UTC 格式返回:

//You can call the format() that will return your UTC date in a string 
 utcStart.format(); 
//Result : 2019-06-24T13:00:00 

但是,如果您按以下方式执行此操作,它将不会转换为 UTC:

var myTime = new moment.utc("09:00", "HH:mm"); 

您只是将输入设置为 UTC 时间,就好像您提到 myTime 是 UTC 时一样,....输出将是 9:00

这将是答案:

moment.utc(moment(localdate)).format()
localdate = '2020-01-01 12:00:00'

moment(localdate)
//Moment<2020-01-01T12:00:00+08:00>

moment.utc(moment(localdate)).format()
//2020-01-01T04:00:00Z
moment.utc(date).format(...); 

是要走的路,因为

moment().utc(date).format(...);

行为很奇怪……

这对我有用。 其他人可能会发现它很有用。

let date = '2020-08-31T00:00:00Z'
moment.utc(moment(date).utc()).format() // returns 2020-08-30T22:00:00Z

如果一切都失败了,只需使用本地偏移量的倒数重新初始化。

var timestamp = new Date();
var inverseOffset = moment(timestamp).utcOffset() * -1;
timestamp = moment().utcOffset( inverseOffset  );

timestamp.toISOString(); // This should give you the accurate UTC equivalent.

moment.utc(stringDate, format).toDate()为我工作。

这个 moment.utc(date).toDate() 不是。

在这里,我传递了日期对象并将其转换为 UTC 时间。

$.fn.convertTimeToUTC = function (convertTime) {
   if($(this).isObject(convertTime)) {
        return moment.tz(convertTime.format("Y-MM-DD HH:mm:ss"), moment.tz.guess()).utc().format("Y-MM-DD HH:mm:ss");
    }
};
// Returns if a value is an object
$.fn.isObject =  function(value) {
    return value && typeof value === 'object';
};


//you can call it as below
$(this).convertTimeToUTC(date);

这对我有用:

const localtime = 1622516400000
moment(localtime).utc(true).format()

在此处阅读 moment.js 的文档。 请参阅下面的示例和输出,其中我将 GMT 时间转换为本地时间(我的区域是 IST),然后我将本地时间转换为 GMT。

// convert GMT to local time
console.log('Server time:' + data[i].locationServerTime)
let serv_utc = moment.utc(data[i].locationServerTime, "YYYY-MM-DD HH:mm:ss").toDate();
console.log('serv_utc:' + serv_utc)
data[i].locationServerTime = moment(serv_utc,"YYYY-MM-DD HH:mm:ss").tz(self.zone_name).format("YYYY-MM-DD HH:mm:ss");
console.log('Converted to local time:' + data[i].locationServerTime)

// convert local time to GMT
console.log('local time:' + data[i].locationServerTime)
let serv_utc = moment(data[i].locationServerTime, "YYYY-MM-DD HH:mm:ss").toDate();
console.log('serv_utc:' + serv_utc)
data[i].locationServerTime = moment.utc(serv_utc,"YYYY-MM-DD HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");
console.log('Converted to server time:' + data[i].locationServerTime)

输出是

Server time:2019-12-19 09:28:13
serv_utc:Thu Dec 19 2019 14:58:13 GMT+0530 (India Standard Time)
Converted to local time:2019-12-19 14:58:13
local time:2019-12-19 14:58:13
serv_utc:Thu Dec 19 2019 14:58:13 GMT+0530 (India Standard Time)
Converted to server time:2019-12-19 09:28:13

这适用于我的情况。

库:“时刻”:“^2.29.1”,

moment().utc().format()

我们可以获得 2 种 UTC 日期格式。

const date = '2021-07-20T18:30:00Z';
moment.utc(moment(date).utc()).format(); // 2021-07-19T18:30:00Z
moment.utc(moment(date).utc()).toISOString(); // 2021-07-20T18:30:00.000Z (Complete ISO-8601)

你不需要一些东西来比较然后检索毫秒吗?

例如:

let enteredDate = $("#txt-date").val(); // get the date entered in the input
let expires = moment.utc(enteredDate); // convert it into UTC

这样,您就有了 UTC 的到期日期。 现在您可以获取 UTC 中的“现在”日期并进行比较:

var rightNowUTC = moment.utc(); // get this moment in UTC based on browser
let duration = moment.duration(rightNowUTC.diff(expires)); // get the diff
let remainingTimeInMls = duration.asMilliseconds();

暂无
暂无

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

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