使用变量的 Javascript Date 对象

标签 javascript html css

我的js代码如下:-

   var earlierdate=new Date(2012,09,22);
   alert(earlierdate.getDay());

   var date2 = new Date('2012,09,22');
   alert(date2.getDay());

问题是第一个警报给我 1 0r 星期一(这是不正确的),第二个警报给我 6 或 sat 这是正确的。因此,引号中给出的日期给出了正确的结果。现在如果想使用变量而不是像

这样的硬编码值
var date1 = new Date(a,b,c);
alert(date1.getDay());

语法应该是什么。我尝试了很多变体但都失败了。

提前致谢。

最佳答案

Datemonth 参数是0-indexed

month

Integer value representing the month, beginning with 0 for January to 11 for December.

因此,如果您指的是 9 月,则应该是 var earlierdate=new Date(2012, 8, 22);

关于使用变量的 Javascript Date 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12543561/

相关文章:

javascript - 尝试让网站添加值

javascript - Bootstrap 模态弹出窗口在 PHP 页面上不起作用 |控制台没有显示错误

html - 为具有多个属性的嵌套元素创建 CSS 选择器

html - Heroku 中的静态 html 应用程序

php - Joomla 错误信息文件目录

javascript - 搜索 javascript 对象数组的更有效方法?

java - 在 Swing 中显示 HTML

css - 2 个相反的不透明度值不会产生不透明的颜色

html - 溢出-y 不工作?

javascript - CSS/JS : How to show text on hover and href clicked text box will show?