Show the date from 2 weeks ago?

Discussion in 'JavaScript' started by Kerosene, Aug 31, 2009.

  1. #1
    How can I use JavaScript to write the date from 2 weeks ago?
     
    Kerosene, Aug 31, 2009 IP
  2. pneulameiro

    pneulameiro Peon

    Messages:
    440
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I found in the net the following script. It can help you.

    <html>
    <head>
    <title>Date substraction</title>
    </head>
    <body>
    <script language="JavaScript" type="text/javascript">
    <!--

    var myVar = prompt("Enter a future date: ")

    var user_date = Date.parse(myVar);
    var today_date = new Date();
    var diff_date = user_date - today_date;

    document.write(diff_date);
    //-->
    </script>
    </body>
    </html>
     
    pneulameiro, Sep 1, 2009 IP
  3. gumape

    gumape Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this sample code, i think it helps you:

    <head>
    	<script>
    		function WriteDateTwoWeeksAgo () {
    			var date = new Date ();
    			var timeInMillisec = date.getTime ();	// the milliseconds elapsed since 01 January, 1970 00:00:00 UTC
    			timeInMillisec -= 14 * 24 * 60 * 60 * 1000; 	// 14 days in milliseconds
    			date.setTime (timeInMillisec);
    
    			var twoWeeksAgoSpan = document.getElementById ("twoWeeksAgo");
    			twoWeeksAgoSpan.innerHTML = date.toLocaleString ();
    		}
    	</script>
    </head>
    <body onload="WriteDateTwoWeeksAgo ()">
    	<span id="twoWeeksAgo"></span>
    </body>
    Code (markup):
     
    gumape, Sep 7, 2009 IP
  4. gumape

    gumape Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you need detailed information, maybe the following link can help you:
    Date object.
     
    gumape, Sep 15, 2009 IP
  5. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #5
    It was easier than I thought:

    var d = new Date();
    with (d) setDate(getDate()-14)
    document.write(d)
     
    Kerosene, Sep 16, 2009 IP