#"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.
About the author
I'm currently working on short form content atClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube@LearnDataWithMark. I previously worked on graph analytics atNeo4j, where I also co-authored theO'Reilly Graph Algorithms Book with Amy Hodler.
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.
About the author
I'm currently working on short form content atClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube@LearnDataWithMark. I previously worked on graph analytics atNeo4j, where I also co-authored theO'Reilly Graph Algorithms Book with Amy Hodler.