日期对象的问题

时间:2012-03-20 13:10:26

标签: javascript

我正在输入字符串,将它们转换为整数(我认为我需要),抓取月份值的前三个字符,小写它,然后将其除以三来定义它是哪个月。 现在令我困惑的部分是Date对象。我认为我做得对,我输入了一切,但它只发送前几个变量的提示,并没有给我一个警告框,显示我在d中存储了什么信息。

var birthYear = parseInt(prompt ('Enter your birth year:'));
var birthMonth = prompt ('Enter the name of the month of birth:');
var birthDay = parseInt(prompt ('Enter your day of birth as an integer:'));
var milliDay = 1000*60*60*24; //Milliseconds in a day
monthAbb = 'janfebmaraprmayjunjulaugsepoctnovdec';
chineseZod = 12;
zodCycle = 1924; //Chinese Zodiac Cycle
var monthArr = new Array(11);


monthArr [0] = "jan";
monthArr [1] = "feb";
monthArr [2] = "mar";
monthArr [3] = "apr";
monthArr [4] = "may";
monthArr [5] = "jun";
monthArr [6] = "jul";
monthArr [7] = "aug";
monthArr [8] = "sep";
monthArr [9] = "oct";
monthArr [10] = "nov";
monthArr [11] = "dec";

var monthNum = monthAbb.indexOf(birthMonth.slice(0, 3).toLowerCase()) / 3;
alert(monthNum);
var d = new Date (birthYear, monthNum, birthDate);
alert(d);

1 个答案:

答案 0 :(得分:2)

在以下行中:

var d = new Date (birthYear, monthNum, birthDate);

birthDateundefined。我认为你的意思是birthDay,就像你之前使用的那样:

var birthDay = parseInt(prompt ('Enter your day of birth as an integer:'));

如果您只是检查浏览器控制台,这种事情通常很容易解决......这是我在Chrome中http://jsfiddle.net上尝试代码时看到的内容的屏幕截图:

enter image description here