www.webdeveloper.com
Results 1 to 13 of 13

Thread: Add 6 months to the date

  1. #1
    Join Date
    Nov 2005
    Posts
    172

    Add 6 months to the date

    How could I output 6 months out from todays date. Currently the script gives me todays date?

    <form name="dform">
    <input type="text" name="currentdate" size=11>
    </form>

    <script>

    var mydate=new Date()
    var theyear=mydate.getYear()
    if (theyear < 1000)
    theyear+=1900
    var theday=mydate.getDay()
    var themonth=mydate.getMonth()+1
    if (themonth<10)
    themonth="0"+themonth
    var theday=mydate.getDate()
    if (theday<10)
    theday="0"+theday

    var displayfirst=themonth
    var displaysecond=theday
    var displaythird=theyear

    document.dform.currentdate.value=displayfirst+"/"+displaysecond+"/"+displaythird
    </script>

  2. #2
    Join Date
    Apr 2006
    Posts
    120
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <title>Untitled</title>
    </head>

    <body>
    <form name="dform">
    Current Date: <input type="text" name="currentdate" size=11><br>
    Future Date: <input type="text" name="futuredate" size=11>
    </form>

    <script>
    var mydate=new Date();
    var fdate=new Date();

    var daysToAdd = 180; //6 months

    var fTime = fdate.getTime()+(daysToAdd*24*60*60*1000);
    fdate.setTime(fTime);

    document.dform.currentdate.value = formatDate(mydate);
    document.dform.futuredate.value = formatDate(fdate);

    function formatDate(d) {
    var theyear=d.getYear()
    if (theyear < 1000) theyear+=1900

    var theday=d.getDay()
    var themonth=d.getMonth()+1
    if (themonth<10) themonth="0"+themonth

    var theday = d.getDate()
    if (theday<10) theday="0"+theday

    var displayfirst=themonth
    var displaysecond=theday
    var displaythird=theyear

    return displayfirst+"/"+displaysecond+"/"+displaythird
    }

    </script>
    </body>
    </html>

  3. #3
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Code:
    <script type="text/javascript">
    
    Date.prototype.addMonths = function (m) {
    	var d = new Date (this.getTime())
    	d.setMonth (d.getMonth() + m)
    	return d
    }
    
    alert (new Date().addMonths(6))
    
    </script>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  4. #4
    Join Date
    Nov 2005
    Posts
    172
    Thanks Guys,
    is it possible to just add the 6 months onto the months and not actually calculate the days so its always is the same day number but just 6 months ahead?


    Thanks!

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by adalby
    Thanks Guys,
    is it possible to just add the 6 months onto the months and not actually calculate the days so its always is the same day number but just 6 months ahead?


    Thanks!
    See my example above.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    Jan 2006
    Posts
    301
    Thanks Charles your output is exactly what I want but I've been trying to insert it into a form field like the above example. Instead of an alert I need to write it into a form box.
    www.unitingbands.com - Where Talents Unite!

  7. #7
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Something like:
    Code:
    <script type="text/javascript">
    
    Date.prototype.addMonths = function (m) {
    	var d = new Date (this.getTime())
    	d.setMonth (d.getMonth() + m)
    	return d
    }
    
    onload = function () {document.forms.elements.dateSixMonthFromNow.value = new Date().addMonths(6)}
    
    </script>
    But if you're not going to be more specific than neither am I.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  8. #8
    Join Date
    Apr 2006
    Posts
    120
    HTML Code:
    <html>
    <head>
    	<title>Untitled</title>
    	<script type="text/javascript">
    	Date.prototype.addMonths = function (m) {
    		var d = new Date (this.getTime())
    		d.setMonth (d.getMonth() + m)
    		return d
    	}
    	
    	onload = function () {
    		document.dform.currentdate.value = formatDate(new Date());
    		document.dform.futuredate.value = formatDate(new Date().addMonths(6));
    	}
    	
    	function formatDate(d) {
    		var theyear=d.getYear()
    		if (theyear < 1000)	theyear+=1900
    			
    		var theday=d.getDay()
    		var themonth=d.getMonth()+1
    		if (themonth<10) themonth="0"+themonth
    		
    		var theday = d.getDate()
    		if (theday<10) theday="0"+theday
    		
    		var displayfirst=themonth
    		var displaysecond=theday
    		var displaythird=theyear
    	
    		return displayfirst+"/"+displaysecond+"/"+displaythird
    	} 
    	</script>
    </head>
    
    <body>
    <form name="dform">
    Current Date: <input type="text" name="currentdate" size=11><br>
    Future Date: <input type="text" name="futuredate" size=11>
    </form>
    </body>
    </html>

  9. #9
    Join Date
    Aug 2004
    Posts
    140
    6 months onto the months and not actually calculate the days
    Just make sure you don't wind up with February 31st or someother type of weirdness.

  10. #10
    Join Date
    Nov 2006
    Posts
    526
    Quote Originally Posted by Watts
    Just make sure you don't wind up with February 31st or someother type of weirdness.
    Feb 31st would evaluate to March 3rd (or March 4th on a leap year).

  11. #11
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by Watts
    Just make sure you don't wind up with February 31st or someother type of weirdness.
    You'll find that my example takes care of that.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  12. #12
    Join Date
    Jan 2006
    Posts
    301
    Perfect guys thank you!
    www.unitingbands.com - Where Talents Unite!

  13. #13
    Join Date
    Jan 2006
    Posts
    301
    Script works great but is it possible to add the 6 months plus the ending day of the month.

    Example:
    todays date = 4-3-2008
    6 month date would be = 10-31-2008

    the 31 or end date would replace todays date(3)

    Thanks for the help
    www.unitingbands.com - Where Talents Unite!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles