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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Time script displaying in eastern time

    Hello, I currently have this time script for my website that displays in pacific time. I would like it to display eastern. I know very little about java and would like some help. Here is the script:

    Code:
    window.setInterval(function(){
    var d =new Date();
    var UTC =  new Date(d.getTime() + (d.getTimezoneOffset()*60000));
    var pstdate = new Date(UTC.getTime() - ((7*60)*60000));
    var hrs=pstdate.getHours();
    var min=pstdate.getMinutes();
    var don="AM"
    if (hrs>=12) {
    don="PM"
    if (hrs>12) { hrs-=12 }
    }
    if (hrs==0) { hrs=12 }
    if (hrs<10) { hrs="0"+hrs }
    if (min<10) { min="0"+min }
    document.getElementById("time").innerHTML=hrs+":"+min+" "+don;
    },1000);
    Thanks.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    1. You don't need to know any Java to be able to write JavaScript - they are two completely different languages.

    2. that code doesn't display any particular timezone - which it displays depends on which timezone the computer the browser is running on is set to. While your computer might be displaying that time as zone -8 my comouter would be displaying it as zone +10 so the adjustment needed to get zone -5 would be to add 3 hours for your computer and to subtract 15 hours for mine. If instead of getting the timezone from the computer and adjusting UTC time by that you instead simply subtract 5 hours instead then provided that the computer the browser is running in has the time and timezone set correctly it would then display zone -5 to everyone. The only way to get it correct for if the computer is set wrong is to feed the time from the server.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    var pstdate = new Date(UTC.getTime() - ((7*60)*60000));

    Just change 7 to 5. But as felgall says it will display EST to every user, even if that user is in Australia. That may not matter to you in your situation.

    Or:-

    Code:
    <script type = "text/javascript">
    
    window.setInterval(function(){
    var dateobj = new Date();
    var hrs  = dateobj.getUTCHours() -5;  // offset GMT - EST
    var min = dateobj.getUTCMinutes();
    var don="AM"
    if (hrs>=12) {don="PM"}
    if (hrs>12) { hrs-=12 }
    if (hrs==0) { hrs=12 }
    if (hrs<10) { hrs="0"+hrs }
    if (min<10) { min="0"+min }
    document.getElementById("time").innerHTML=hrs+":"+min+" "+don;
    },1000);
    
    </script>
    But you will need to take account of DST where appropriate.
    Last edited by Philip M; 08-12-2013 at 08:18 AM.

    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.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Philip M View Post
    var pstdate = new Date(UTC.getTime() - ((7*60)*60000));

    Just change 7 to 5. But as felgall says it will display EST to every user, even if that user is in Australia. That may not matter to you in your situation.
    um, isn't the east coast 3 hours back from the pacific time zone?

    7 pacific
    6 mountain
    5 central
    4 eastern
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by rnd me View Post
    um, isn't the east coast 3 hours back from the pacific time zone?

    7 pacific
    6 mountain
    5 central
    4 eastern
    Oh, I am not an American. I am sure you are right. So change 7 to 4.

    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.

  • #6
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did want it to display EST to everyone, I am running a server and I wanted it to display the server time, which is EST. Thank you everyone for your help.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    If you want it to display the server time, then you will need to *SEND* the server time *TO THE BROWSER*.

    Oh, you can get the browser to display *approximately* the right time, but how do you know that the clock on a given user's computer isn't 5 seconds off? or 5 minutes off? Or 5 days off? (I know some people who purposely change the time/date on their computer because they are trying to use software for free after the free license period runs out.)

    So if you want to accurately reflect server time, then you need to use PHP/JSP/ASP/etc. on the server to send the server time to the browser, where you then ignore the user's clock time.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Old Pedant View Post
    So if you want to accurately reflect server time, then you need to use PHP/JSP/ASP/etc. on the server to send the server time to the browser, where you then ignore the user's clock time.
    well, the pages on this forum emit a Date header, which can be used via AJAX's HEAD request to grab the server time without any server code like php or asp.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • Users who have thanked rnd me for this post:

    Old Pedant (08-13-2013)

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    LOL! Neat trick, RndMe!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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