比较 javascript/jquery 中的两种日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18913579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare two date formats in javascript/jquery
提问by Roy M J
I have the following :
我有以下几点:
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?
上面的代码不起作用。上述问题还有其他解决方案吗?
formatting date as per Where can I find documentation on formatting a date in JavaScript?also dint work for me.
按照格式格式化日期我在哪里可以找到有关在 JavaScript 中格式化日期的文档?也对我有用。
回答by Elias Van Ootegem
It's quite simple:
这很简单:
if(new Date(fit_start_time) <= new Date(fit_end_time))
{//compare end <=, not >=
//your code here
}
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.
比较 2 个Date
实例将工作得很好。它只会valueOf
隐式调用,将Date
实例强制为整数,可以使用所有比较运算符进行比较。好吧,要 100% 准确:Date
实例将被强制转换为Number
类型,因为 JS 不知道整数或浮点数,它们都是有符号的 64 位 IEEE 754 双精度浮点数。
回答by Amal Kumar S
To comapre dates of string format (mm-dd-yyyy).
比较字符串格式的日期 (mm-dd-yyyy)。
var job_start_date = "10-1-2014"; // Oct 1, 2014
var job_end_date = "11-1-2014"; // Nov 1, 2014
job_start_date = job_start_date.split('-');
job_end_date = job_end_date.split('-');
var new_start_date = new Date(job_start_date[2],job_start_date[0],job_start_date[1]);
var new_end_date = new Date(job_end_date[2],job_end_date[0],job_end_date[1]);
if(new_end_date <= new_start_date) {
// your code
}
回答by shennan
As in your example, the fit_start_time
is not later than the fit_end_time
.
在您的示例中, thefit_start_time
不晚于fit_end_time
.
Try it the other way round:
反过来试试:
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.");
}
Update
更新
Your code implies that you want to see the alert
with the current variables you have. If this is the case then the above code is correct. If you're intention (as per the implication of the alert message
) is to make sure their fit_start_time
variable is a date that is beforethe fit_end_time
, then your original code is fine, but the data you're getting from the jQuery .val()
methods is not parsing correctly. It would help if you gave us the actual HTML which the selector is sniffing at.
您的代码意味着您想查看alert
您拥有的当前变量。如果是这种情况,那么上面的代码是正确的。如果您的意图(根据警报的含义message
)是确保它们的fit_start_time
变量是在 之前的日期fit_end_time
,那么您的原始代码没问题,但是您从 jQuery.val()
方法获得的数据没有正确解析. 如果您向我们提供选择器正在嗅探的实际 HTML,将会有所帮助。
回答by NaYaN
try with new Date(obj).getTime()
尝试 new Date(obj).getTime()
if( new Date(fit_start_time).getTime() > new Date(fit_end_time).getTime() )
{
alert(fit_start_time + " is greater."); // your code
}
else if( new Date(fit_start_time).getTime() < new Date(fit_end_time).getTime() )
{
alert(fit_end_time + " is greater."); // your code
}
else
{
alert("both are same!"); // your code
}
回答by ch.smrutiranjan parida
This is already in :
这已经在:
Age from Date of Birth using JQuery
or alternatively u can use Date.parse() as in
或者你可以使用 Date.parse() 作为
var date1 = new Date("10/25/2011");
var date2 = new Date("09/03/2010");
var date3 = new Date(Date.parse(date1) - Date.parse(date2));
回答by user2775080
Try it the other way:
换一种方式试试:
var start_date = $("#fit_start_time").val(); //05-09-2013
var end_date = $("#fit_end_time").val(); //10-09-2013
var format='dd-MM-y';
var result= compareDates(start_date,format,end_date,format);
if(result==1)/// end date is less than start date
{
alert('End date should be greater than Start date');
}
OR:
if(new Date(start_date) >= new Date(end_date))
{
alert('End date should be greater than Start date');
}