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 3 of 3

Thread: Javascript Time

  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    76
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Thumbs up Javascript Time

    hi guys. i need something that can work out what date and time im getting from a 3rd party site. The way it comes to me is like this: 1342461600

    It will have to work out a formula based around the fact that i know the following:

    I know that is 1342461600 = 19:00 16th July 2012
    I also know that 1342548000 = 19:00 17th July 2012

    I know that the difference is 86400 seconds which is 1440 minutes which is 24 hours. As you can see, the number grows every second by 1

    i know basicly that the question/formula is if 1342461600 = 19:00 16th July 2012 what does 1342548000 = ???

    but i dont have a clue how to write it!!

    So when i recieve the string in the url like &date=1342461600 and can place it within your code like this :
    Code:
    '+date+'
    i need to convert it and print it in the correct format. I would prefer if i can break it up to print it seperately like

    Code:
    '+hour+' 
    '+min+'
    '+day+'
    '+month+'
    '+year+'
    Dont worry about daylight savings, thats managed at source or i can reset the formula if its not.

    If its not too much to ask it would be nice if it could handle 'st' , 'nd' , 'rd' and 'th' too eg: 1st, 2nd, 4th
    maybe:
    Code:
    if(day == "1"){
    	document.write(''+day+'st');
    }else if(day == "2"){
    	document.include(''+day+'nd');
    }
    And so on
    Tho im sure thats quite alot more long winded than it needs to be!



    it would also need to know that 30 days have september, april, june and november etc!!


    Anyone up to the challange?

    Cheers Guys
    Last edited by embeebutterly; 07-19-2012 at 03:33 PM. Reason: Resolved By Phill

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    This is a well-trodden path. You ought to study the Javascript Date object.

    Code:
    <script type = "text/javascript">
    
    var secs = 1342461600; // a value in seconds - Javascript epoch time since 1/1/1970 GMT
    var ms = secs * 1000;  // make milliseconds
    var d = new Date(ms);  // create a new Date object
    var year = d.getFullYear();
    var month = d.getMonth();
    var date = d.getDate();
    var hrs = d.getHours();
    if (hrs <10) {hrs = "0" + hrs}
    var mins = d.getMinutes();
    if (mins <10) {mins = "0" + mins}
    var day = d.getDay();
    var mth=['January','February','March','April','May','June','July','August','September','October','November','December'];
    var dy = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    suffix = (date==1 || date==21 || date==31) ? "st" : "th" && (date==2 || date==22) ? "nd" : "th" && (date==3 || date==23) ? "rd" : "th";
    alert (hrs + ":" + mins + " " + dy[day] + "," + " " + date + suffix  + " " + mth[month] + "," + " " + year);
    document.write(hrs + ":" + mins + " " + dy[day] + "," + " " + date + suffix  + " " + mth[month] + "," + " " + year);
    
    
    </script>
    Obviously you can format the output to meet your precise requirements.

    If you are 20 and you are not a socialist, then you have no heart. If you are 40 and you are still a socialist, then you have no brain.
    Last edited by Philip M; 07-19-2012 at 02:41 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    embeebutterly (07-19-2012)

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    76
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thank you very much, this is perfect! yes its true i have alot to learn.


  •  

    Posting Permissions

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