使用moment .js,我无法将正确的moment对象转换为带时区的date对象。我记不出准确的日期。

例子:

var oldDate = new Date(),
    momentObj = moment(oldDate).tz("MST7MDT"),
    newDate = momentObj.toDate();
console.log("start date " + oldDate)
console.log("Format from moment with offset " + momentObj.format())
console.log("Format from moment without offset " + momentObj.utc().format())
console.log("(Date object) Time with offset " + newDate)
console.log("(Date object) Time without offset "+ moment.utc(newDate).toDate())

当前回答

new Date(moment()) -在excel中导出数据列时可能会出错

use

moment.toDate() -不会给出错误或使导出的文件损坏

其他回答

我需要在日期字符串中包含时区信息。我最初用的是瞬间。tz (dateStr美国/ New_York) .toString ();但后来我开始在把弦送回时刻时出错。

我尽力了。tz (dateStr美国/ New_York) .toDate ();但是后来我丢失了我需要的时区信息。

返回带有时区的可用日期字符串的唯一解决方案是moment。tz (dateStr美国/ New_York) .format ();

let dateVar = moment('any date value');
let newDateVar = dateVar.utc().format();

很干净!!!!

转换任何日期,例如utc:

moment( moment().utc().format( "YYYY-MM-DD HH:mm:ss" )).toDate()

由于momentjs无法控制javascript日期对象,我找到了一个解决这个问题的方法。

const currentTime = new Date(); const convertTime = moment(currentTime).tz(timezone)。格式(“YYYY-MM-DD HH: mm: ss”); const convertTimeObject = new Date(convertTime);

这将给你一个javascript日期对象与转换后的时间

Moment已于2018年6月更新了js库。

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00

如果你有使用Angular5+的自由,那么最好使用datePipe特性而不是timezone函数。我必须使用moment.js,因为我的项目仅限于Angular2。