[英]Can not compare date using Angular.js
I need one help.I need to compare todays date with future date using Angular.js . 我需要一个帮助。我需要使用Angular.js比较今天的日期和将来的日期。 I am explaining my code below.
我在下面解释我的代码。
$scope.getFutureDate = function() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
var today = dd + '-' + mm + '-' + yyyy;
return today;
}
$scope.addPlanData = function() {
var today = $scope.getFutureDate();
if (today < $scope.date) {
alert('Please select todays date or previous date');
}
}
Here my requirement is when todays date will less than future date it will show me the alert message.In this code its happening only within the month.But if i will compare with like this 30-11-2015 with 1-12-2015
,its not showing any alert message.Please help me. 在这里,我的要求是当今天的日期小于将来的日期时,它将向我显示警报消息。在此代码中,它仅在一个月内发生。但是如果我将
30-11-2015 with 1-12-2015
日30-11-2015 with 1-12-2015
日进行比较,它没有显示任何警报消息。请帮助我。
You need to manipulate string to create date. 您需要操纵字符串来创建日期。
Try this 尝试这个
$scope.addPlanData=function(){
var today= new Date();
if(today < new Date($scope.date.split("-").reverse().join(",")){
alert("Please select today's date or previous date");
}
}
It gives you alert when user select future date which is greater than today. 当用户选择的将来日期大于今天时,它会给您警报。 If you want alert when user select past date then use
>
in if condition. 如果要在用户选择过去日期时发出警报,则在条件中使用
>
。
Try this 尝试这个
$scope.addPlanData=function(){
var today= new Date();
var dateArray = $scope.date.split("-");
$scope.date = new Date(dateArray[2], dateArray[1] - 1, dateArray[0]);
if(today < $scope.date){
alert('Please select today or previous date');
}
}
The relational operators <
<=
>
>=
can be used to compare JavaScript dates. 关系运算符
<
<=
>
>=
可用于比较JavaScript日期。
When you compare dates as strings, the comparison takes one char at a time, and because in your example 3 is greater than 1 (even if 30-11-2015
is a smaller date than 1-12-2015
), the result is greater for the november date. 当您比较日期字符串,比较需要一个字符的时间,因为在你的例子3大于1(即使
30-11-2015
比较小的日期1-12-2015
),其结果是更大11月的日期。 If you want to stick to string representations of dates, change the format of date to YYYY-MM-DD
, so that you'll have the year compared first, then the month, then the day. 如果要坚持使用日期的字符串表示形式,请将日期格式更改为
YYYY-MM-DD
,以便首先比较年份,然后是月份,然后是日期。 In this situation you'll have 2015-12-01
greater than 2015-11-30
, because the comparison will stop at the 2
from the units digit of the month. 在这种情况下,你将有
2015-12-01
大于2015-11-30
,因为比较会在停止2
从该月的个位数。
$scope.getFutureDate = function () {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if ( dd < 10 ) {
dd = '0' + dd;
}
if ( mm < 10 ) {
mm = '0' + mm;
}
return yyyy + '-' + mm + '-' + dd;
}
$scope.addPlanData = function () {
var today = $scope.getFutureDate();
if ( today < $scope.date ) {
alert('Please select todays date or previous date');
}
}
You can compare dates directly. 您可以直接比较日期。 In this care you need to cast your
$scope.date
variable to Date
format by $scope.date = new Date($scope.date)
and compare it with new Date()
. 在这种情况下,您需要通过
$scope.date = new Date($scope.date)
将$scope.date
变量转换为Date
格式,并将其与new Date()
进行比较。 This method gives you less code, as you don't need the getFutureDate
method. 由于不需要
getFutureDate
方法,因此此方法可以减少代码getFutureDate
。
$scope.addPlanData = function () {
if ( new Date() < new Date($scope.date) ) {
alert('Please select todays date or previous date');
}
}
It seems like you want to parse a Date from a custom format ( dd-mm-yyyy
). 似乎您想从自定义格式(
dd-mm-yyyy
)解析Date。
I'd recommend to use a dedicated function to parse that date format, especially if you need to parse that kind of date format in more than this case: 我建议使用专用的函数来解析该日期格式,特别是如果您需要解析这种日期格式的情况不止这种情况:
function parseDate(dateString) {
var date = new Date();
// reset time to 0:00:00
date.setUTCHours(0);
date.setUTCMinutes(0);
date.setUTCSeconds(0);
var parts = dateString.split("-");
date.setUTCDate(parts[0]);
date.setUTCMonth(parts[1]);
date.setUTCFullYear(parts[2]);
return date;
}
And use it like this: 并像这样使用它:
$scope.addPlanData = function() {
var today = new Date();
if (today < parseDate($scope.date)) { // parse custom date format here
alert('Please select todays date or previous date');
}
}
You can use regular expression to do the same. 您可以使用正则表达式执行相同的操作。
Convert the string to date using regular expression and then use < or > or <= or >=
to compare. 使用正则表达式将字符串转换为日期,然后使用
< or > or <= or >=
进行比较。
$scope.compare = function() {
var date1 = "30-11-2015";
var date2 = "1-12-2015";
var d1 = new Date( date1.replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3") );
var d2 = new Date( date2.replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3") );
alert(d1 < d2);
};
Reference: Stackoverflow 参考: Stackoverflow
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.