JavaScript-比较不同格式的日期 [英] javascript - compare dates in different formats

查看:69
本文介绍了JavaScript-比较不同格式的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个日期需要比较,以查看一个日期是否大于另一个日期,但是它们的格式不同,我不确定比较2个日期的最佳方法。

I have 2 dates which I need to compare to see if one is greater than the other but they are in different formats and I'm not sure of the best way to compare the 2.

格式为:

1381308375118 (这是var futureDate)

1381308375118 (this is var futureDate)

var今天创建= new Date(); today.setHours(0,0,0,0); var futureDate = new Date()。setDate(today.getDate()+ 56); //将来的56天...

另一种格式是

2013/08/26

有什么想法可以比较2吗?

Any ideas how I can compare the 2?

推荐答案

如果不使用第三方库,则可以使用这两种格式创建新的 Date 对象,使用 getTime()检索毫秒数(自1970年1月1日午夜开始),然后只需使用>

Without using a 3rd party library, you can create new Date objects using both those formats, retrieve the number of milliseconds (since midnight Jan 1, 1970) using getTime() and then simply use >:

new Date("2013/08/26").getTime() > new Date(1381308375118).getTime()

这篇关于JavaScript-比较不同格式的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆