2014-04-28 3 views
0

입니다. 주어진 날짜가 과거인지 미래인지 비교하고 싶습니다.
주어진 날짜가 yyyy-mm-dd 형식의 문자열에서 제공됩니다.
나는 오늘을 "고르고"주어진 날짜 날짜의 시간대를 보완하려고 노력했지만 더 나은 방법이 있어야한다고 확신합니까 ??자바 스크립트의 과거 날짜 유효 기간이

var today   = new Date(); 
console.log("TODAY: " + today); // Mon Apr 28 2014 14:46:41 GMT+0200 (CEST) 
var todayYear  = today.getFullYear(); 
var todayMonth  = today.getMonth(); 
todayMonth   = parseInt(todayMonth, 10) + 1; 
var todayDay  = today.getDate(); 
var todayFormatted = todayYear + "-" + todayMonth + "-" + todayDay; 
today    = new Date(todayFormatted); 
console.log("TODAY: " + today); // Mon Apr 28 2014 00:00:00 GMT+0200 (CEST) 
var testDate  = new Date("2014-04-28"); 
console.log("TEST: " + testDate); // Mon Apr 28 2014 02:00:00 GMT+0200 (CEST) 
testDate.setHours(00); 
console.log("TEST: " + testDate); 
// check if test is in the past 
(testDate < today) ? console.log('test is in the past') : console.log('test is NOT in the past'); 

답변

0

당신은 수학적으로 "유닉스 시간"을 비교할 수 있습니다

var today = new Date(); 
console.log(testDate.getTime() < today.getTime() 
? 'test is in the past' 
: 'test is NOT in the past'); 
관련 문제