• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date - calulate new to x number of months?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What I need to do is to be able to calculate, with JavaScript, a new date that is x number of months out from an existing date. But my requirement is a little odd - i.e. if the current date is January 31, and I need to move it out by a parameter of 1 month, what my company wants is the new Date to be February 28 (or 29 if a leap year). If the current date is January 15th then it is a bit simpler - the new date would be February 15th. But if the current date is January 31 and the parameter is 2 months the new date should be March 31. (It is not enough to say just move it out 30 or 60 or whatever days - which I already know how to do). Can this be calculated in JavaScript and if so, how?

Many Thanks
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not an expert in Javascript but I think the easiest way to get the job done is to hardcode all these exception cases.

The javascript Date object doesn't really check what you set, e.g. you can set the date as 31/2 or 0/2. Therefore, doing a getDate() - 1 won't work.

see web page
 
Chit Ming Chong
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just googled about how to get the last day of the month including heap years. I only found hardcoded implementation see here
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I came up with:



Eric
 
Chit Ming Chong
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a clear piece of code. It works mostly. However, It doesn't work when the I try 3/30/2005 or 3/1/2005. They returned 4/29/2005 and 3/31/2005 respectively. May be it is moving the date by an offset of 30 days instead of 1 month. In that case, the code works perfectly.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the case of moving it exactly one month

change

dateE.setDate(dateE.getDate()+30);

to

dateE.setMonth(dateE.getMonth()+1);

I think that is what what Weston wanted the 1 month added, but I got the idea in my head of 30 days.

Eric
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric, I like the idea of setting the day to 0 to arrive at the last day of the previous month. Is that a common trick, because it's the first I've seen of it.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I been doing it a lot. Other ways of doing it is to set it to the first day of the month and then subtract one when doing setDate().

try making 32 days in January and see what the date is. It is how the date functionality works. Makes it easier than trying to do a big leap year calc.

Eric
 
Keith Sebastian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had previously written some javascript that used the subtract technique, but I like the 0 way. Thanks.
 
Weston Kornfeld
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many Thanks!
 
Keith Sebastian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Sebastian:
I had previously written some javascript that used the subtract technique, but I like the 0 way. Thanks.



Maybe I'm the only one who finds this funny, but after I wrote it, I realized that subtracting until getting to the previous month is the same as just setting the date to 0. In fact looping/subtracting is a waste of processing. It's amusing how such obvious things could be overlooked.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic