Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Customized Clock/Date

    Hi,

    I'm pretty much a newbie when it comes to javascript and I have a small request for anyone who is willing to help me on this.

    All I need is a simply date script, but a bit customized.

    Basicly the script should show date as "January 1, 2009" in a website, but it wouldn't show the actual date. It would show a custom date.

    To calculate the date shown, three dates are used:

    D1 (date 1) is the current date
    D2 is a pre-set date (for example 1.1.1990)
    D3 is also a pre-set date (eg 1.1.2000)

    The final date is D1-D2 (difference between the two dates...in days), multiplied by X (a positive whole number) and added to D3 (date 3).


    For the sake of example, if:
    D1 is today (29. January 2009)
    D2 is 28. January 2009
    D3 is 1. January 2000
    and the number X has a value of 2

    the final date would be 3. January 2000. The difference between D1 and D2 is 1 day, multiplied by 2...equals two days. Added to the D3 makes it January 3rd.


    Thanks anyways

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Here you are:-

    Code:
    <script type = "text/javascript">
    var months = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December");
    var x = 2;
    var now = new Date();   // today
    var D1 = now.getTime();
    var D2 = now.setFullYear(2009,0,28);  //  YYYY,MM,DD  -  months are 0-11
    var diff = D1 - D2;
    diff = diff * x;
    var D3 = now.setFullYear(2000,0,1);  // months are 0-11
    var finalDate = D3+diff;
    var d = new Date(finalDate);
    var mm = d.getMonth();
    var dd = d.getDate();
    var yy = d.getFullYear();
    finalDate = (dd + " " + months[mm] + " " + yy);  
    
    alert (finalDate);  // 3 January 2000
    </script>

    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” Shunryu Suzuki (Japanese Zen priest, ?-1971)
    Last edited by Philip M; 01-29-2009 at 08:19 AM. Reason: Typo


  •  

    Posting Permissions

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