2014-12-07 100 views
0

力圖打造從用戶的Date對象輸入出生日期,然後getDay自今日起他們進入:創建從用戶輸入一個日期對象

MONTHS = 'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'; //months 

var dateString = prompt('Enter your birth date in the form dd mmm yyyy','26 APR 1988'); //get users date of birth 
var substrings = dateString.split(' '); 
var day = substrings[0]; 
var month = substrings[1]; 
var year = substrings[2]; 

var pos = MONTHS.indexOf(MONTHS); 
if (pos >= 0) { 
} 

alert(MONTHS.indexOf(month)/3); 

var daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; 
var daysOfWeek = new Date('day','month','year'); 
document.writeln(daysOfWeek.getDay());{ 

我不知道要放什麼東西在新日期對象,我試了幾件事,我完全偏離軌道?

+0

'{'這是一個錯字'document.writeln(daysOfWeek.getDay()); {' – Trialcoder 2014-12-07 11:32:46

回答

0

構造函數Date的語法有些不同。嘗試以下方法:

var MONTHS = ["JAN", "FEB", ..., "DEC"]; 
var monthNum = MONTHS.indexOf(month); 
var birthDate = new Date(year, monthNum, day); 

注意monthNum是零指數(即0 =一月,1 =二月,...,11 =分解)。