2011-09-14 39 views
0

JavaScript檢查日期不是過去或現在。JavaScript檢查日期不是過去或過去

var c = '10 JUN 2010'; // this is the format of the date coming in. 
var temp = new Array(); 
temp = c.split(' '); 

var x = new Date (temp[1]+" "+temp[0]+", "+temp[2]); 

if (x.getTime() > getDate()) { 
    alertstring = alertstring + '\n\nDEBUG CODE: 1 ' + x + '\n\n'; 
} 

我不能改變的格式在未來

+2

請問一個問題。 –

+0

看看'Date.parse'。你不需要做那個分割。 'var x = new Date(Date.parse(c))' – Hemlock

回答

2

看起來你99%的擁有它。這是一個改進,如果條件:

var c = '10 JUN 2010'; // this is the format of the date coming in. 
var temp = new Array(); 
temp = c.split(' '); 

var x = new Date (temp[1]+" "+temp[0]+", "+temp[2]); 

if (x.getTime() > (new Date().getTime())) { 
    ... 
} 
+0

唯一不好的事情是我讀的拆分在IE7中可能行不通,那是真的嗎? –

1

更新這一行:

// Get current date and time 
var today = new Date(); 

// strip time to compare to the parse date 
if (x.getTime() > new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime()) { 
    // this date has not happened yet. 
    alertstring = alertstring + '\n\nDEBUG CODE: 1 ' + x + '\n\n'; 
} 
0

嘗試在構造函數中放一個月 的數量而不是字符串。 而不是6月份放6個。