简体   繁体   English

如何在正确的时刻指定时区和日期对象格式,以便我不会收到无效日期和不推荐使用的时刻警告?

[英]How do I specify the timezone and date object format on moment correct such that I do not get Invalid date and a moment is deprecated warning?

I am trying to set the date format and timezone and then when I try to format moment to extract the date it just returns Invalid date.我正在尝试设置日期格式和时区,然后当我尝试格式化 moment 以提取日期时,它只返回无效日期。

When I do it this way I do not get moment is deprecated.当我这样做时,我没有得到过时的时刻。

let data = moment(date, 'YYYY-MM-DD HH:MM').tz(TIMEZONE); //moment('2014-06-01 12:00:00Z').tz(TIMEZONE); // TIMEZONE = 'America/New_York'
console.log(data.format('MM/DD/YYYY'))
let value = inventoryBase.lastUpdated + SPACE + data.format('MM/DD/YYYY') + ' at ' + data.format('LT') + SPACE + moment.tz([2012, 0], TIMEZONE).zoneAbbr();
value = value + ' by ' + lastUpdated.editedBy;

But the output of value is incorrect:但是 value 的输出不正确:

Last updated Invalid date at Invalid date EST by System

When I do it this way I get moment is deprecated.当我这样做时,我得到的时刻已被弃用。

let data = moment(date).tz(TIMEZONE); //moment('2014-06-01 12:00:00Z').tz(TIMEZONE);
let value = inventoryBase.lastUpdated + SPACE + data.format('MM/DD/YYYY') + ' at ' + data.format('LT') + SPACE + moment.tz([2012, 0], TIMEZONE).zoneAbbr();
value = value + ' by ' + lastUpdated.editedBy;

But the output of value is correct:但是 value 的输出是正确的:

Last updated 05/01/2019 at 3:59 AM EST by System

Moment is deprecated error:不推荐使用时刻错误:

Deprecation warning: value provided is not in a recognized RFC2822 or ISO 
format. moment construction falls back to js Date(), which is not reliable 
across all browsers and versions. Non RFC2822/ISO date formats are 
discouraged and will be removed in an upcoming major release. Please refer 
to http://momentjs.com/guides/#/warnings/js-date/ for more info.

Try the following:请尝试以下操作:

const data = moment.tz(date, 'YYYY-MM-DD HH:mm:ssZ', TIMEZONE);
const value = `${inventoryBase.lastUpdated} ${data.format('L [at] LT zz')} by ${lastUpdated.editedBy}`;

A few other points:其他几点:

  • In your original code, it didn't work correctly because your format string didn't match the format of the data.在您的原始代码中,它无法正常工作,因为您的格式字符串与数据格式不匹配。 MM is months, while mm is minutes, and you didn't include seconds. MM是月,而mm是分钟,您没有包括秒。

  • In your second attempt, you got the deprecation warning because you provided data in a format that isn't in the list of known supported strings .在您的第二次尝试中,您收到了弃用警告,因为您提供的数据格式不在已知支持的字符串列表中

  • You were taking the zone abbreviation for an arbitrary date instead of the one in question.您将区域缩写用于任意日期而不是相关日期。 Time zone abbreviations vary by date, so it's better to get it from the same moment you're working with.时区缩写因日期而异,因此最好从您使用的同一时刻开始获取。

  • It's not good to mix tokenized formats ( MM/DD/YYYY ) and locale-aware formats ( LT ) in the same output string.在同一个输出字符串中混合标记化格式( MM/DD/YYYY )和区域设置感知格式( LT )是不好的。 I used L instead, as it gets the date format for the locale you're working with.我改为使用L ,因为它获取您正在使用的语言环境的日期格式。

  • Because you have a Z in your data, it will always be treated as UTC.因为您的数据中有一个Z ,所以它将始终被视为 UTC。 Thus you can skip right to parsing it with moment.tz .因此,您可以直接跳过使用moment.tz解析它。 If you didn't have the Z , then you would go through moment.utc(data, format).tz(timezone) instead.如果您没有Z ,那么您将通过moment.utc(data, format).tz(timezone)代替。

  • I used JavaScript template literals in my answer because I think they are cleaner than concatenation for cases like this.我在回答中使用了 JavaScript 模板文字,因为我认为对于这种情况,它们比串联更清晰。

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

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