SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Date subtractor

    Hi,

    I have a date function that subtract a certain amount of days from todays date.
    Code:
    function printDate(offset) 
    {
    
    	var offsetAmount	= offset
    	var currentTime		= new Date()
    	var month		= currentTime.getMonth() + 1
    	var day			= currentTime.getDate()
    	var nDay		= day - offsetAmount
    	var year		= currentTime.getFullYear()
    	
    	document.write(nDay + "/" + month + "/" + year)
    
    }
    I need it to be able to recognise when it goes into a previous month (ie instead of 0/6/2006 it will go to 31/5/2006)

    Any help would be greatly appreciated.

  2. #2
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can nobody help? Is it really that difficult?

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you're making it hard for yourself.

    function printDate(offset) {
    var myDate=new Date();
    myDate.setDate(myDate.getDate()-offset);
    var month=myDate.getMonth()+1;
    var day=myDate.getDate();
    var year=myDate.getFullYear();
    document.write(day + "/" + month + "/" + year);
    }

  4. #4
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i was indeed, thanks for clearing it up, much appreciated.


Bookmarks

Posting Permissions

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