简体   繁体   English

使用Angular.js / Javascript无法正确比较日期

[英]Can not compare dates properly using Angular.js/Javascript

I need one help.I am unable to compare the selected date with today's date using Angular.js or JavaScript.I am explaining my code below. 我需要一个帮助。我无法使用Angular.js或JavaScript将所选日期与今天的日期进行比较。我在下面解释我的代码。

var today=new Date();
console.log('2 dates',today,$scope.date);

From the above console i am getting the output like below. 从上面的控制台,我得到如下的输出。

2 dates Fri Jun 03 2016 18:29:16 GMT+0530 (India Standard Time) 03-06-2016

if(today > new Date($scope.date.split("-").reverse().join(","))){
    alert('Please select  future delivery date');
}

Here i need suppose my selected date is 03-06-2016 and today's date is 03-06-2016 the alert prompt should not come.But in my case its not happening like this.This alert message is coming also if selected date is 03-06-2016 . 在这里,我需要想我选择的日期是03-06-2016和今天的日期是03-06-2016它没有发生像this.This警报消息也即将如果选择的日期是警报提示不应该在我的情况come.But 03-06-2016 Here i need the alert message should come if selected date is more than today's date only.Please help me. 如果选择的日期仅比今天的日期多,我需要在这里发出警报消息。请帮助我。

Months are zero-indexed when constructing Dates: 构造Date时,月份是零索引的:

console.log(new Date(2016,06,03));

"2016-07-03T04:00:00.000Z" // you expected June, but got July.

Note also that that is set to midnight: any new Date() run during the same day will be greater than that value. 还要注意将其设置为午夜:同一天运行的任何new Date()都将大于该值。

String manipulation of dates is risky and error-prone, which is why everyone's knee-jerking momentjs at you (it is in fact a very good library and worth using if you're doing much date manipulation). 日期的字符串操作是冒险且容易出错的,这就是每个人都为您服务的原因(实际上,它是一个非常好的库,如果您要进行大量的日期操作,则值得使用)。

At the least, you may want to consider using a less ambiguous date format in $scope.date , but if you're stuck with that, in this case, you need to subtract 1 from the month when constructing a Date from it: 至少,您可能要考虑在$scope.date使用不太模糊的日期格式,但是如果您坚持使用这种格式,在这种情况下,在从中构造Date时,您需要从月份中减去1:

 // this sample code will only work correctly on June 3 2016 :) var $scope = { date: "03-06-2016", // I am assuming DD-MM-YYYY format here tomorrow: "04-06-2016" }; var today = new Date(); // test against today: var dArr = $scope.date.split('-'); var testDate = new Date(dArr[2],dArr[1]-1,dArr[0]); if (today > testDate) { console.log("today is greater than testDate."); /* Note that anytime during today will be greater than the zero o'clock constructed from $scope.date. If what you really want is to alert only on the following day, then add 1 to the day of $scope.date when converting it to a Date(). */ } // now test against tomorrow: dArr = $scope.tomorrow.split('-'); testDate = new Date(dArr[2],dArr[1]-1,dArr[0]); if (today < testDate) { console.log("today is less than (tomorrow's) testDate."); } 

Don't compare JavaScript dates using native < or > operators; 不要使用本机<或>运算符比较JavaScript日期; install and use moment.js - it will handle all the difficulties for you: 安装并使用moment.js-它会为您解决所有困难:

if(moment(new Date()).isAfter(moment($scope.date, "MM-DD-YYYY"), 'day')) { ...

Convert to timestamp to compare it: 转换为timestamp进行比较:

$scope.date = '2016-06-01';
var currentDate = new Date().valueOf();
var date = new Date($scope.date).valueOf();

if (currentDate > date) {
  // something
}

It you use multi timezone , please convert both to a timezone . 如果您使用multi timezone ,请将两者都转换timezone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM