1

伙计们,我需要在当前日期前一周约会,以便我可以在客户到期前一周向他们发送电子邮件警报。

这是我为当前日期编写的代码

  var d = new Date();

  var month = d.getMonth()+1;
  var day = d.getDate();

  var current_date = d.getFullYear() + '/' +
  ((''+month).length<2 ? '0' : '') + month + '/' +
  ((''+day).length<2 ? '0' : '') + day;

  alert(current_date);

所以我需要像当前日期减去一周这样的日期。提前感谢您的任何帮助。

4

3 回答 3

4

Try this

var today=new Date(); 
var lastWeekDate = new Date(today.setDate(today.getDate() - 7)));

alert(lastWeekDate);

JsFiddle Demo

If you want to format the date you can write your own function like this

function formatDate(d){
  function pad(n){return n<10 ? '0'+n : n}
  return d.getUTCFullYear()+'/'+ pad(d.getMonth()+1) +'/'+ pad(d.getDate())
}

and call it like this

var formattedDate = formateDate(lastWeekDate) // returns `2013/10/19` 
于 2013-10-26T07:32:43.813 回答
1
var today = new Date();
var lastWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);

EDIT

var oldDate = lastWeek.getFullYear() + "/"+ lastWeek.getMonth() +"/"+lastWeek.getDate();
于 2013-10-26T07:31:58.820 回答
0

Using timestamp would be more efficient and easier. 7 days millisecconds: 7*24*60*1000=10080000 So if you store the users expiration date stamp t, you would only have to check:

if(new Date.getTime()>=t) alert("expired");
else if(new Date.getTime()+10080000>=t) alert("notification");
else alert("all good");
于 2013-10-26T07:38:42.167 回答