JavaScript : 将本地时区日期对象转换为 UTC 时区日期对象的优雅方式
JavaScript : Elegant way to convert local timezone date object to UTC timezone date object
我想将具有本地时间戳的 Date
对象转换为具有 UTC 时间戳的 Date
"object"。我找到的一种方法如下:
var d = new Date(); //gives Date object with local timezone
var s_str_utc = d.toUTCString(); //converts it to UTC timezone but
//is no longer an object but a string
是否有直接转换为对象的函数。我总是可以使用正则表达式和其他东西从 s_str_utc
获取日期和时间,但这不是一种优雅的方式。
var moment = require("moment");
var d = moment(newDate);
var utc = d.utc();
我想将具有本地时间戳的 Date
对象转换为具有 UTC 时间戳的 Date
"object"。我找到的一种方法如下:
var d = new Date(); //gives Date object with local timezone
var s_str_utc = d.toUTCString(); //converts it to UTC timezone but
//is no longer an object but a string
是否有直接转换为对象的函数。我总是可以使用正则表达式和其他东西从 s_str_utc
获取日期和时间,但这不是一种优雅的方式。
var moment = require("moment");
var d = moment(newDate);
var utc = d.utc();