javascript 如何以 dd/mm/yyyy 格式比较两个不同的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6055256/
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
How to compare two different dates in dd/mm/yyyy format
提问by Raghu
Can anyone help me in finding the solution
谁能帮我找到解决方案
i just want to compare two dates in dd/mm/yyyy format.
我只想比较 dd/mm/yyyy 格式的两个日期。
function compareDate(dt1 , dt2 , formatString){var returnVal = 2;
var dt1Parts;
var dt2Parts;
var dt1dd;
var dt1mm;
var dt1yyyy;
var dt2dd;
var dt2mm;
var dt2yyyy;
if(formatString == 'dd/mm/yyyy'){
dt1Parts = dt1.split('/');
dt2Parts = dt2.split('/');
dt1dd = parseInt(dt1Parts[0]);
dt1mm = parseInt(dt1Parts[1]);
dt1yyyy = parseInt(dt1Parts[2]);
dt2dd = parseInt(dt2Parts[0]);
dt2mm = parseInt(dt2Parts[1]);
dt2yyyy = parseInt(dt2Parts[2]);
}
else if(formatString == 'dd-mm-yyyy'){
dt1Parts = dt1.split('-');
dt2Parts = dt2.split('-');
dt1dd = parseInt(dt1Parts[0]);
dt1mm = parseInt(dt1Parts[1]);
dt1yyyy = parseInt(dt1Parts[2]);
dt2dd = parseInt(dt2Parts[0]);
dt2mm = parseInt(dt2Parts[1]);
dt2yyyy = parseInt(dt2Parts[2]);
}else{
alert(formatString+' format is not supported.');
}
if(dt1yyyy == dt2yyyy && dt1mm == dt2mm && dt1dd == dt2dd){
returnVal = 0;
}
else if(dt1yyyy > dt2yyyy){
returnVal = 1 ;
}else if(dt1yyyy == dt2yyyy ){
if(dt1mm > dt2mm){
returnVal = 1;
}else if(dt1mm == dt2mm){
if(dt1dd > dt2dd){
returnVal = 1;
}else{
returnVal = -1;
}
}else{
returnVal = -1;
}
}else{
returnVal = -1;
}
return returnVal;
}
Thanks in advance, Shilpa
提前致谢,希尔帕
回答by jcomeau_ictx
Invert the strings to yyyy/mm/dd, or convert them to a number or Date object.
将字符串反转为 yyyy/mm/dd,或将它们转换为数字或日期对象。
The simplest way just for comparison would be ASCII order. Invert using something like this:
仅用于比较的最简单方法是 ASCII 顺序。使用这样的方法反转:
function invert(date) {
return date.split(/[/-]/).reverse().join("")
}
function compareDates(date1, date2) {
return invert(date1).localeCompare(invert(date2));
}
回答by Samuli Hakoniemi
Here's how you convert that string format to a date:
以下是将该字符串格式转换为日期的方法:
var myString = "17/07/1979",
correctFormat = myString.replace(/(\d+)\/(\d+)\/(\d+)/, "//"),
myDate = new Date(correctFormat);
回答by Kevin Hsu
Without knowing what language or class libs you're working with:
不知道您正在使用什么语言或类库:
Method 1: Resort your strings to be yyyymmdd and the do string compare. Method 2: Stuff yyyy mm and dd into the high, middle, and low bits of an integer and compare.
方法 1:使您的字符串成为 yyyymmdd 并进行字符串比较。方法二:将yyyy mm和dd填入一个整数的高、中、低位并进行比较。
回答by Jayantha Lal Sirisena
Try this
试试这个
var date1=new Date('your date1 string');
var date2=new Date('your date2 string');
var difference=new Date(date1.getTime()-date2.getTime());
回答by TweeZz
The easiest way is probably to create 2 javascript Date objects from your input string. You could achieve that by chopping your input into day, month and year. You can use the 'substring' function for that.
最简单的方法可能是从您的输入字符串创建 2 个 javascript Date 对象。您可以通过将输入分成日、月和年来实现这一目标。您可以为此使用“子字符串”函数。
Then you can do:
然后你可以这样做:
var firstDate = new Date(year1, month1, day1);
var secondDate = new Date(year2, month2, day2);
Once you have 2 date objects, you can use the normal compare operators:
一旦你有 2 个日期对象,你就可以使用普通的比较运算符:
if (firstDate > secondDate)
// do something
else
...
回答by shafi7468
if ($.datepicker.parseDate('dd/mm/yy', fDate) > $.datepicker.parseDate('dd/mm/yy', tDate)) {
//do something
}
You can compare two dates.Here I compare from date greater than to date
try this
你可以比较两个日期。在这里我比较从大于到日期的日期
试试这个