jQuery/JS-如何比较两个日期/时间戳? [英] jQuery/JS - How to compare two date/time stamps?

查看:548
本文介绍了jQuery/JS-如何比较两个日期/时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期/时间戳记:

I have two date/time stamps:

d1 = 2011-03-02T15:30:18-08:00 
d2 = 2011-03-02T15:36:05-08:00

我想在上面比较两者:

if (new Date(d1) < new Date(d2)) {alert('newer')}

但是,这似乎无法正常工作.有没有一种方法不仅可以比较日期,还可以比较时间.谢谢

But that does not appear to be working correctly. Is there a way to compare not just the dates but the times as well.? thanks

更新:

console.log(d1 + ' ' + d2);
console.log(new Date(d1) > new Date(d2))


2011-03-02T15:30:18-08:00 2011-03-02T15:36:05-08:00
false
2011-03-02T15:30:18-08:00 2011-03-02T15:30:18-08:00
false
2011-03-02T15:30:18-08:00 2011-03-02T14:15:04-08:00
false

推荐答案

您的时间戳应该是字符串.

Your timestamps should be strings.

var d1 = "2011-03-02T15:30:18-08:00";
var d2 = "2011-03-02T15:36:05-08:00";

if (new Date(d1) < new Date(d2)) {alert('newer')}

示例: http://jsfiddle.net/hKPkF/

这篇关于jQuery/JS-如何比较两个日期/时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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