我在脚本中使用以下内容:
var startDate = new Date("10/12/2012");
var endDate = new Date("10/18/2012");
我希望动态创建这些日期,startDate 为上周一,endDate 为上周日。我已经尝试过以下方法:
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first)).format("m/dd/yyyy");
var endDate = new Date(curr.setDate(last)).format("m/dd/yyyy");
但由于某种原因,这不起作用 - 不会为 startDate 或 endDate 变量输出任何内容。
有什么想法我做错了吗?
Javascript 日期时间对象没有格式化方法。您需要使用库或自己生成字符串:
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first));
startDate = "" + (startDate.getMonth() + 1) + "/" + startDate.getDate() + "/" + startDate.getFullYear();
var endDate = new Date(curr.setDate(last));
endDate = "" + (endDate.getMonth() + 1) + "/" + endDate.getDate() + "/" + endDate.getFullYear();
这是一个小提琴http://jsfiddle.net/DPQeB/2/及其输出
2012年11月18日
2012年11月24日
一个允许您格式化日期的库是jQuery用户界面.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)