比较javascript / jquery中的两种日期格式 [英] Compare two date formats in javascript/jquery

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

问题描述

我有以下内容:

var fit_start_time  = $("#fit_start_time").val(); //2013-09-5
var fit_end_time    = $("#fit_end_time").val(); //2013-09-10

if(Date.parse(fit_start_time)>=Date.parse(fit_end_time)){
    alert("Please select a different End Date.");
}

上述代码不起作用。是否还有其他解决方案?

The above code doesn't work. Is there any other solution to the above one?

格式化日期我在哪里可以找到有关格式化JavaScript日期的文档?也适合我。

推荐答案

这很简单:

if(new Date(fit_start_time) <= new Date(fit_end_time))
{//compare end <=, not >=
    //your code here
}

比较2 日期实例将正常工作。它只是隐式地调用 valueOf ,将 Date 实例强制转换为整数,可以使用所有比较运算符进行比较。好吧,要100%准确: Date 实例将被强制转换为 Number 类型,因为JS没有知道整数或浮点数,它们都是64位IEEE 754双精度浮点数。

Comparing 2 Date instances will work just fine. It'll just call valueOf implicitly, coercing the Date instances to integers, which can be compared using all comparison operators. Well, to be 100% accurate: the Date instances will be coerced to the Number type, since JS doesn't know of integers or floats, they're all signed 64bit IEEE 754 double precision floating point numbers.

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

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