开发者

Javascript, Time and Date: Getting the next day, week, month, year, etc

Based on a given millisecond ti开发者_运维技巧mestamp, what's the 'correct' way to get the next day, week, month, year, etc.? That is, without having to do some kind of binary search with raw millisecond timestamp values or something silly like that.

Edit: Does using the Date constructor with a month, day, hour, etc. value beyond the limit translate it to the next year, month, day, etc.?


function getNextDate()
  { 
   var today = new Date();
   var d = today.getDate();
   var m = today.getMonth();
   var y = today.getYear();

   var NextDate= new Date(y, m, d+1);
   var Ndate=NextDate.getMonth()+1+"/"+NextDate.getDate()+"/"+NextDate.getYear();
   alert(Ndate);
   }


If the millisecond timestamp that you have is (conveniently!) the number of milliseconds since 1970/01/01 then you can simply create a new Date object from the millisecond value new Date(milliseconds) and use it as outlined in Misnomer's answer.

If your timestamp is based from onther point in time then you can simply workout the offset (in milliseconds) from 1970/01/01 and subtract that from the timestamp before creating the Date object.

As always when dealing with dates, be clear if you are dealing in local or UTC times.

w3schools date object

w3schools full date reference

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜