如何在javascript中比较两个字符串日期?

时间:2022-05-21 14:18:46

I have two string dates in the format of m/d/yyyy. For example, “11/1/2012”, “1/2/2013”. I am writing a function in JavaScript to compare two string dates. The signature of my function is bool isLater(string1, string2), if the date passed by string1 is later than the date passed by string2, it will return true, otherwise false. So, isLater(“1/2/2013”, “11/1/2012”) should return true. How do I write a JavaScript function for this?

我有两个字符串日期格式为m / d / yyyy。例如,“11/1/2012”,“1/2/2013”​​。我在JavaScript中编写一个函数来比较两个字符串日期。我的函数的签名是bool isLater(string1,string2),如果string1传递的日期晚于string2传递的日期,则返回true,否则返回false。因此,isLater(“1/2/2013”​​,“11/1/2012”)应该返回true。如何为此编写JavaScript函数?

5 个解决方案

#1


50  

var d1 = Date.parse("2012-11-01");
var d2 = Date.parse("2012-11-04");
if (d1 < d2) {
    alert ("Error!");
}

Demo Jsfiddle

#2


5  

Parse the dates and compare them as you would numbers:

解析日期并按照数字进行比较:

function isLater(str1, str2)
{
    return new Date(str1) > new Date(str2);
}

If you need to support other date format consider a library such as date.js.

如果您需要支持其他日期格式,请考虑使用诸如date.js之类的库。

#3


2  

You can simply compare 2 strings

您可以简单地比较2个字符串

function isLater(dateString1, dateString2) {
  return dateString1 > dateString2
}

Then

isLater("2012-12-01", "2012-11-01")

returns true while

返回true时

isLater("2012-12-01", "2013-11-01")

returns false

#4


0  

If your date is not in format standar yyyy-mm-dd (2017-02-06) for example 20/06/2016. You can use this code

如果您的日期格式不符合标准yyyy-mm-dd(2017-02-06),例如20/06/2016。您可以使用此代码

var parts ='01/07/2016'.val().split('/');
var d1 = Number(parts[2] + parts[1] + parts[0]);
parts ='20/06/2016'.val().split('/');
var d2 = Number(parts[2] + parts[1] + parts[0]);
return d1 > d2

#5


0  

Directly parsing a date string that is not in yyyy-mm-dd format, like in the accepted answer does not work. The answer by vitran does work but has some JQuery mixed in so I reworked it a bit.

直接解析不是yyyy-mm-dd格式的日期字符串,就像在接受的答案中一样不起作用。 vitran的答案确实有效,但有一些JQuery混入,所以我稍微改了一下。

// Takes two strings as input, format is dd-mm-yyyy
// returns true if d1 is smaller than or equal to d2

function compareDates(d1, d2){
var parts =d1.split('/');
var d1 = Number(parts[2] + parts[1] + parts[0]);
parts = d2.split('/');
var d2 = Number(parts[2] + parts[1] + parts[0]);
return d1 <= d2;
}

P.S. would have commented directly to vitran's post but I don't have the rep to do that.

附:会直接评论vitran的帖子,但我没有代表那样做。

#1


50  

var d1 = Date.parse("2012-11-01");
var d2 = Date.parse("2012-11-04");
if (d1 < d2) {
    alert ("Error!");
}

Demo Jsfiddle

#2


5  

Parse the dates and compare them as you would numbers:

解析日期并按照数字进行比较:

function isLater(str1, str2)
{
    return new Date(str1) > new Date(str2);
}

If you need to support other date format consider a library such as date.js.

如果您需要支持其他日期格式,请考虑使用诸如date.js之类的库。

#3


2  

You can simply compare 2 strings

您可以简单地比较2个字符串

function isLater(dateString1, dateString2) {
  return dateString1 > dateString2
}

Then

isLater("2012-12-01", "2012-11-01")

returns true while

返回true时

isLater("2012-12-01", "2013-11-01")

returns false

#4


0  

If your date is not in format standar yyyy-mm-dd (2017-02-06) for example 20/06/2016. You can use this code

如果您的日期格式不符合标准yyyy-mm-dd(2017-02-06),例如20/06/2016。您可以使用此代码

var parts ='01/07/2016'.val().split('/');
var d1 = Number(parts[2] + parts[1] + parts[0]);
parts ='20/06/2016'.val().split('/');
var d2 = Number(parts[2] + parts[1] + parts[0]);
return d1 > d2

#5


0  

Directly parsing a date string that is not in yyyy-mm-dd format, like in the accepted answer does not work. The answer by vitran does work but has some JQuery mixed in so I reworked it a bit.

直接解析不是yyyy-mm-dd格式的日期字符串,就像在接受的答案中一样不起作用。 vitran的答案确实有效,但有一些JQuery混入,所以我稍微改了一下。

// Takes two strings as input, format is dd-mm-yyyy
// returns true if d1 is smaller than or equal to d2

function compareDates(d1, d2){
var parts =d1.split('/');
var d1 = Number(parts[2] + parts[1] + parts[0]);
parts = d2.split('/');
var d2 = Number(parts[2] + parts[1] + parts[0]);
return d1 <= d2;
}

P.S. would have commented directly to vitran's post but I don't have the rep to do that.

附:会直接评论vitran的帖子,但我没有代表那样做。