SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Need help with Javascript date script

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

    Need help with Javascript date script

    Hi,

    I have a list of jobs with dates like:

    Date Posted: 01/05/06

    I need a javascript that will take today's date and subtract how ever many days it has been since the date above and output

    Date Posted: 4 Days Ago.

    I have no idea about how to go about this, as I dont have much of a background in JS.

    Can anyone help me out?

    Cheers,
    Spudz.

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I've changed what I want for this I've decided that all I want to do is print a date which if printDate(0) is set Today's date will be printed and if printDate(1) is set Yesterdays date will be printed.

    So far I have
    Code:
    function printDate(offset) {
    	var offsetAmount	= offset.value
    	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)
    }
    Can anyone point me in the right direction here, I havent a clue what I'm doin really

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    15,824
    Mentioned
    8 Post(s)
    Tagged
    1 Thread(s)
    The following is amended to correctly subtract the number of days from the current date (to handle change of month).

    Code:
    function printDate(offset) {
    var offsetAmount= offset.value
    var currentTime= new Date()
    currentTime.setDate(currentTime.getDate() - offsetAmount);
    var month= currentTime.getMonth() + 1
    var day= currentTime.getDate()
    var year= currentTime.getFullYear()
    
    document.write(nDay + "/" + month + "/" + year)
    }
    The other thing you need to look at is that document.write can only be used before the page finishes loading so you if it is goint to be run after that then you also need to replace that line either with an innerHTML call to update a part of the current page or by using the DOM methods to add the extra field into the page.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

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
  •