简体   繁体   English

Moment ISO 弃用控制台警告

[英]Moment ISO Deprecation console Warning

I am facing the below warning,我面临以下警告,

Deprecation warning: value provided is not in a recognized ISO format.弃用警告:提供的值不是公认的 ISO 格式。 moment construction falls back to js Date(), which is not reliable across all browsers and versions. moment 构造回退到 js Date(),这在所有浏览器和版本中都不可靠。 Non ISO date formats are discouraged and will be removed in an upcoming major release.不鼓励使用非 ISO 日期格式,并将在即将发布的主要版本中将其删除。 Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.请参阅http://momentjs.com/guides/#/warnings/js-date/了解更多信息。 Arguments: [0] _isAMomentObject: true, _isUTC: true, _useUTC: true, _l: undefined, _i: 2016-9-26 19:30, _f: undefined, _strict: undefined, _locale: [object Object] Arguments: [0] _isAMomentObject: true, _isUTC: true, _useUTC: true, _l: undefined, _i: 2016-9-26 19:30, _f: undefined, _strict: undefined, _locale: [object Object]

When trying to do this using moment,当尝试使用 moment 执行此操作时,

let startDate = "2022-11-29T11:30:00Z"
let currentDate = moment().format("MM-DD-YYYY HH:mm")

I need to find the difference between the startDate and currentDate in hours.我需要以小时为单位找出 startDate 和 currentDate 之间的差异。 So firstly i tried formatting startDate like this,所以首先我尝试像这样格式化 startDate,

let formatStartDate = moment(startDate).format("MM-DD-YYYY HH:mm")

And tried finding diff like this,并尝试找到这样的差异,

let diffTime = moment(formatStartDate).diff(currentDate,"hours").

So, how should I refactor this code and get rid of that warning, any suggestions please?那么,我应该如何重构这段代码并摆脱那个警告,有什么建议吗?

You re-format the parsed start date right again and use the formatted string for the comparison.您再次重新格式化解析的开始日期,并使用格式化的字符串进行比较。 Remove the .format(...) calls altogether or move formatting to the latest possible place, if you need to print the time:如果您需要打印时间,请完全删除.format(...)调用或将格式移动到最新的可能位置:

// set the current date in the IST timezone (+05:30)
let currentDate = moment().utcOffset(5*60 + 30);

// set the start date for comparison, also in IST timezone
let startDate = moment("2022-11-29T11:30:00Z").utcOffset(5*60 + 30);

// get the difference between the two as number in hours
let diffTime = startDate.diff(currentDate, "hours");

console.log(`There are ${diffTime} hours between ${startDate.format()}
and ${currentDate.format()}.`);

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

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