我试图在javascript中获取上周的日期,没有时间.
例如,10-02-2012,而不是10-02-12 13:34:56 GMT.
那有一个简单的解决方案吗?
谢谢!
编辑:
我试图让这个动态,所以结果变量总是在当前日期之前一周.这是我为计算今天变量而做的,如果这有用或可以使用的话!
var currentTime = new Date(); var month = currentTime.getMonth() + 1 var day = currentTime.getDate(); var year = currentTime.getFullYear(); var today = month + "-" + day + "-" + year; alert(today)
解决方法
我更喜欢这样的东西
function getLastWeek(){ var today = new Date(); var lastWeek = new Date(today.getFullYear(),today.getMonth(),today.getDate() - 7); return lastWeek ; } var lastWeek = getLastWeek(); var lastWeekMonth = lastWeek.getMonth() + 1; var lastWeekDay = lastWeek.getDate(); var lastWeekYear = lastWeek.getFullYear(); var lastWeekDisplay = lastWeekMonth + "/" + lastWeekDay + "/" + lastWeekYear; var lastWeekDisplayPadded = ("00" + lastWeekMonth.toString()).slice(-2)+ "/" + ("00" + lastWeekDay .toString()).slice(-2)+ "/" + ("0000" + lastWeekYear .toString()).slice(-4); alert(lastWeekDisplay); alert(lastWeekDisplayPadded);
如果您使用的是jQuery UI,则可以执行此操作而不是手动步骤来构建字符串
var lastWeekDisplay = $.datepicker.formatDate('mm/dd/yy',getLastWeek());
或者今天
var todayDisplay = $.datepicker.formatDate('mm/dd/yy',new Date());