本文介绍了比较javascript中的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将日期保存为以下格式的字符串: 2017-09-28T22:59:02.448804522Z 该值由后端服务提供.

I have a date saved in a string with this format: 2017-09-28T22:59:02.448804522Z this value is provided by a backend service.

现在,在javascript中,如果该时间戳大于当前时间戳,该如何比较?我的意思是,我需要知道时间是否发生了,而不仅仅是小时数.

Now, in javascript how can I compare if that timestamp is greater than the current timestamp? I mean, I need to know if that time happened or not yet taking in count the hours and minutes, not only the date.

推荐答案

您可以解析它以创建 Date 的实例并使用内置的比较器:

You can parse it to create an instance of Date and use the built-in comparators:

new Date('2017-09-28T22:59:02.448804522Z') > new Date()
// true
new Date('2017-09-28T22:59:02.448804522Z') < new Date()
// false

方便地,日期已经是 Date 知道如何解析的标准格式(看起来像ISO8601).

Conveniently, the date is already in an standard format that Date knows how to parse (looks like ISO8601).

这篇关于比较javascript中的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 10:45