Moment.js转换为日期对象

本文翻译自:Moment.js transform to date object

Using Moment.js I can't transform a correct moment object to a date object with timezones. 使用Moment.js,我无法将正确的矩对象转换为具有时区的日期对象。 I can't get the correct date. 我找不到正确的日期。

Example: 例:

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())

#1楼

参考:https://stackoom.com/question/1DTPL/Moment-js转换为日期对象


#2楼

As long as you have initialized moment-timezone with the data for the zones you want , your code works as expected. 只要您使用所需区域的数据初始化了moment-timezone ,您的代码就会按预期工作。

You are correctly converting the moment to the time zone, which is reflected in the second line of output from momentObj.format() . 您正在正确地将矩转换为时区,这反映在momentObj.format()的第二行输出中。

Switching to UTC doesn't just drop the offset, it changes back to the UTC time zone. 切换到UTC不仅会减少偏移量,还会改回到UTC时区。 If you're going to do that, you don't need the original .tz() call at all. 如果要这样做,则根本不需要原始的.tz()调用。 You could just do moment.utc() . 你可以做moment.utc()

Perhaps you are just trying to change the output format string? 也许您只是想更改输出格式字符串? If so, just specify the parameters you want to the format method: 如果是这样,只需为format方法指定所需的参数:

momentObj.format("YYYY-MM-DD HH:mm:ss")

Regarding the last to lines of your code - when you go back to a Date object using toDate() , you are giving up the behavior of moment.js and going back to JavaScript's behavior. 关于代码的最后几行-当您使用toDate()返回Date对象时,您将放弃moment.js的行为并返回到JavaScript的行为。 A JavaScript Date object will always be printed in the local time zone of the computer it's running on. JavaScript Date对象将始终在运行它的计算机的本地时区中打印。 There's nothing moment.js can do about that. 瞬间没有任何办法。js对此无能为力。

A couple of other little things: 其他一些小事情:

  • While the moment constructor can take a Date , it is usually best to not use one. 尽管构造函数可以使用Date ,但通常最好不要使用Date For "now", don't use moment(new Date()) . 对于“ now”,请不要使用moment(new Date()) Instead, just use moment() . 相反,只需使用moment() Both will work but it's unnecessarily redundant. 两者都可以,但是不必要地多余。 If you are parsing from a string, pass that string directly into moment. 如果您要从字符串中进行解析,请将该字符串直接传递给moment。 Don't try to parse it to a Date first. 不要尝试先将其解析为Date You will find moment's parser to be much more reliable. 您会发现瞬间的解析器更加可靠。

  • Time Zones like MST7MDT are there for backwards compatibility reasons. MST7MDT这样的时区在这里是出于向后兼容的原因。 They stem from POSIX style time zones, and only a few of them are in the TZDB data. 它们源于POSIX样式的时区,并且其中只有少数位于TZDB数据中。 Unless absolutely necessary, you should use a key such as America/Denver . 除非绝对必要,否则应使用America/Denver类的密钥。


#3楼

Use this to transform a moment object into a date object: 使用它可以将矩对象转换为日期对象:

From http://momentjs.com/docs/#/displaying/as-javascript-date/ http://momentjs.com/docs/#/displaying/as-javascript-date/

moment().toDate();

Yields: 产量:

Tue Nov 04 2014 14:04:01 GMT-0600 (CST)

#4楼

.toDate did not really work for me, So, Here is what i did : .toDate并不是真的为我工作,所以,这就是我所做的:

futureStartAtDate = new Date(moment().locale("en").add(1, 'd').format("MMM DD, YYYY HH:MM"))

hope this helps 希望这可以帮助


#5楼

I needed to have timezone information in my date string. 我需要在日期字符串中包含时区信息。 I was originally using moment.tz(dateStr, 'America/New_York').toString(); 我最初使用的是moment.tz(dateStr, 'America/New_York').toString(); but then I started getting errors about feeding that string back into moment. 但是后来我开始收到有关将字符串反馈回去的错误。

I tried the moment.tz(dateStr, 'America/New_York').toDate(); 我尝试了moment.tz(dateStr, 'America/New_York').toDate(); but then I lost timezone information which I needed. 但是后来我丢失了我需要的时区信息。

The only solution that returned a usable date string with timezone that could be fed back into moment was moment.tz(dateStr, 'America/New_York').format(); 返回具有时区的可用日期字符串的唯一解决方案是可以返回到moment的方法是moment.tz(dateStr, 'America/New_York').format();


#6楼

moment has updated the js lib as of 06/2018. 此刻已更新了截至2018年6月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

if you have freedom to use Angular5+, then better use datePipe feature there than the timezone function here. 如果您可以自由使用Angular5 +,那么最好使用此处的datePipe功能,而不要使用此处的时区功能。 I have to use moment.js because my project limits to Angular2 only. 我必须使用moment.js,因为我的项目仅限于Angular2。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值