Movatterモバイル変換


[0]ホーム

URL:


Mark Needham

Mark Needham

·javascriptdatejs

#"javascript">var jan312009 = new Date(2009, 1-1, 31);var oneMonthFromJan312009 = new Date(new Date(jan312009).setMonth(jan312009.getMonth()+1));

The output of these two variables is:

Sat Jan 31 2009 00:00:00 GMT+1100 (EST)Tue Mar 03 2009 00:00:00 GMT+1100 (EST)

Not quite what we want!

Luckily there is a library calleddatejs which has taken care of this problem for us. It provides a really niceDSL which makes it very easy for us to do what we want.

We can add a month to a date very easily now:

var jan312009 = new Date(2009, 1-1, 31);var oneMonthFromJan312009 = new Date(jan312009).add(1).month();
Sat Jan 31 2009 00:00:00 GMT+1100 (EST)Sat Feb 28 2009 00:00:00 GMT+1100 (EST)

There are loads of other useful date manipulation functions which you can read more about on theAPI, just don’t forget that date in Javascript is mutable so any manipulation done to dates contained in vars will change the original value.


[8]ページ先頭

©2009-2025 Movatter.jp