我需要得到从字符串格式的get请求到现在的日期时间的差异(以分钟为单位)。
根据我的研究,我可以使用moment.js来做到这一点,但我现在还没有搞清楚。
我要比较的日期/时间格式是:
2017年-02-10T20:52:13.885Z
我已经尝试过使用moment.js执行一些操作,例如
moment().startof(comparedTime).fromNow())
但什么也没回。
有什么选择和最好的方法来做到这一点?
发布于 2017-02-10 13:17:14
你就不能用香草javaScript吗?
var getDate = '2017-02-10T20:52:13.885Z'; //get time from server
var parseDate = new Date(getDate).getTime(); //change string into Date object into milliseconds
var nowDate = Date.now(); //get current Date in milliseconds
var minutes = Math.round((nowDate-parseDate)/1000/60); //subtract times, count seconds (/1000), count minutes (/60)
console.log(minutes);
发布于 2017-02-10 13:11:37
您需要通过传递日期字符串来创建矩对象。例如:
myDate = moment(myISOString)
https://momentjs.com/docs/#/parsing/
然后,您可以使用文档中描述的矩对象。
发布于 2017-02-10 13:56:11
对于Moment.js,这只是简单的:
moment().diff('2017-02-10T20:52:13.885Z', 'minutes') // 65
如果希望包含部分分钟,那么将true
作为第三个参数传递:
moment().diff('2017-02-10T20:52:13.885Z', 'minutes', true) // 65.04565
https://stackoverflow.com/questions/42168692
复制