2016-10-19 3 views
0

문제가 있습니다. Angular.js/Javascript를 사용하여 오늘 날짜를 오늘 날짜와 비교할 수 없습니다. 아래 코드를 설명하고 있습니다. 여기Angular.js/Javascript를 사용하여 날짜를 비교할 수 없습니다.

var today=new Date(); 
if(today >= new Date($scope.date1.split("-").reverse().join(","))){ 
    alert('Please select todays date or upcoming date'); 
} 

나는 날짜가 선택한 날짜는 경고가 도와 display.Please 것을 오늘 날짜 이전 날짜가있는 동안 내가 필요 2016-10-18라도 좋습니다 동안 내가 비교할 수없는이 2016-10-18 format.Here 같은 $scope.date1 값을 얻고있다.

+0

' "2016년 10월 18일"유사해야합니다 귀하의 경우 그래서 false

anothertoday > today //---->false 

로 평가한다 .split ('-'). reverse(). join (',')'은 (는) 18,10,2016 "'어떤 종류의 날짜 형식도 아닙니다. – Phil

+0

'new Date ('2016-10-18')'올바른 올바른 'Date' 인스턴스를 만드는 것처럼 보입니다. 당신이 무엇을 달성하려고하는지 전혀 모르겠다. – Phil

+0

@ Phil : 그러나 나는 '2016-10-06'과 같은 데이트를하고 있지만, 잘 작동한다. – subhra

답변

1

new Date($scope.date1.split("-").reverse().join(","))은 유효한 날짜를 만들지 않습니다. 당신이하고있는 것처럼

//Pass the string directly 
 
var nDate = new Date('2016-10-18'); 
 
var today = new Date(); 
 
if (today >= nDate) { 
 
    console.log('Please select todays date or upcoming date'); 
 
}

+0

@ Satpal : 입력 된 짝수 경고도 오늘날에는 오지 말아야하는 'ie-2016-10-19'날짜입니다. – subhra

+0

@subhra, 시간도 고려됩니다.'new Date()'는 현재 날짜와 시간을'new date ('2016-10-18')'이 시간대 오프셋 인 – Satpal

+0

으로 날짜를 설정할 것으로 생각합니다. 이 작업을 수행. 여기에는 날짜 만 입력됩니다. – subhra

0

당신은 날짜를 비교할 수 없습니다.

new Date()으로 날짜 개체를 초기화하면 현재 시간으로 설정됩니다.

today.getHours() //---> 0 
anothertoday.getHours() //---> current time shall be displayed 

그것을 비교하기 위해 당신이 날짜 모두의 시간을 볼 경우 때문에 그래서

var today = new Date("2016-10-19"); //----> current date here 
var anothertoday = new Date(); 

은 위의 표현식이 true로 평가 같은

anothertoday > today //-----> true 

수 없을 것이다 date만을 기준으로 anothertoday 시간을 설정해야합니다. 이제 anothertoday.setHours(0,0,0,0)

에 의해 0에 위의 표현은 코드가 알다시피이

var today = new Date(); 
$scope.date1 = "2016-10-18"; 
$scope.datearr = $scope.date1.split("-"); 
var yesterday = new Date($scope.datearr[0],$scope.datearr[1] - 1,$scope.datearr[2]); 
today.setHours(0,0,0,0); //----> set time to 0 hours 
if(today > yesterday) 
    console.log("Please select todays date or upcoming date"); 
관련 문제