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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    May 2012
    Location
    Australia
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Current date/time in JavaScript

    Hi guys.

    I'm trying to make a small bit of scripting that will show the current time, updating once every second.

    What I need is the following:
    1. Multiple timezones or GMT offsets (just put me on the right path, I can code them in,
    2. Daylight savings time taken into consideration (only for Australia), adding the 1 hour during the times it is needed,
    3. Text to show "Open" and "Closed" during different times (open during 8am to 5pm and closed if not 8am to 5pm.)

    I have the following code as a start, but I am completely stuck with how to get the rest of what I need!

    Any help would be appreciated.


    <h4>Victoria
    <script type="text/javascript">
    <!--
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    if (minutes < 10){
    minutes = "0" + minutes
    }
    document.write(hours + ":" + minutes + " ")
    if(hours > 11){
    document.write("PM")
    } else {
    document.write("AM")
    }
    //-->
    </script>


    <script type="text/javascript">
    <!--
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    if (minutes < 10){
    minutes = "0" + minutes
    }
    if(hours > 11){
    document.write("True")
    } else {
    document.write("False")
    }
    //-->
    </script>
    </h4>


    Cheers in advance.

    Dale.

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    881
    Thanks
    0
    Thanked 115 Times in 114 Posts
    converting to AM/PM is not that easy.

    1<= hours AM/PM <=12.
    So you can't say 15:46 PM.
    It is 03:46 PM.

    you need to find out when the number for the hours is less or more than the 24-hours clock.

    00:00 - 00:59 = 12:00 AM - 12:59 AM.
    so here you will have to add 12 to get the right hours.
    whereas if the hours are 13 or higher, you wil have to substract 12.
    13:30 = 01:30 PM.

    You are right that hours from 0-11 is AM and 12-13 is PM.

    To get the right notation:
    Code:
    suffix='AM'
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    if (minutes < 10){
    minutes = "0" + minutes
    }
    if (hours = 0){hours = 12}
    if (hours > 12){hours = hours -12}
    if (hours > 11){suffix = 'PM'}
    document.write(hours+':'+minutes+' '+suffix);

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Here is an open/closed script (courtesy Old Pedant):

    Code:
    <html>
    <head>
    <style type="text/css">
    #openSign.OPEN {
        color: green;
        background-color: yellow;
        font-size: x-large;
    }
    #openSign.CLOSED {
        color : red;
        background-color: pink;
        font-size: large;
    }
    </style>
    
    <script type="text/javascript">
    
    var OPENAT = 7.5; // 7:30 AM ... change as appropriate - can be fractions of an hour e.g. 7.5 = 7:30am
    var CLOSEAT = 21; // 9:00 PM ... change as appropriate
    
    function areWeOpen( ) {
    var sign = document.getElementById("openSign");
    var day = new Date().getDay();
    var hour = new Date().getHours();
    var mins = new Date().getMinutes();
    hour = hour + mins/60;
    if ( day >=1 && day <=5 && hour >= OPENAT && hour < CLOSEAT )  {
    sign.innerHTML = "We are now OPEN";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "Sorry, we are now CLOSED";
    sign.className = "CLOSED";
    }
    }
    </script>
    </head>
    
    <body onload = "areWeOpen()">
    <center>
    <h2>Welcome to our Store</h2>
    <br/>
    <h3 id="openSign"></h3>
    <br/>
    </center>
    ... rest of page ...
    </body>
    </html>
    Quizmaster: How many players are there in a septet?
    Contestant: Well, September is the ninth month so it might be nine. I'll go for five.
    Last edited by Philip M; 05-05-2012 at 08:19 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.

  • Users who have thanked Philip M for this post:

    daleala (05-10-2012)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    And here is a script to show the date/time on your page:-

    Code:
    <html>
    <head>
    <script type = "text/javascript">
    
    function startTime() {
    var now = new Date();
    var h=now.getHours();
    var min=now.getMinutes();
    var s=now.getSeconds();
    var ampm=(now.getHours()>11)?"PM":"AM";
    var d=now.getDay();
    var y=now.getFullYear();
    var mon=now.getMonth();
    var dm=now.getDate();
    var endings=["st","nd","rd","th"];
    var dayendings=[0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,0,2,3,4,4,4,4,4,4,1];
    var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var months=["January","February","March","April", "May", "June","July","August","September", "October", "Novemeber","Decemeber"];
    if (h>12) {h-=12}
    if (h==0) {h=12};
    if (min<10) {min="0"+min}
    if (s<10) {s="0"+s}
    dm+=endings[dayendings[dm]];
    if (dm<10) {dm="0"+dm};
    d=days[d];
    mon=months[mon-1];
    document.getElementById("time").innerHTML=h+":"+min+":"+s+" "+ampm+" "+d+" "+mon+" "+dm+" "+y;
    var tim = setTimeout("startTime()",1000);
    }
    
    </script>
    </head>
    <body onload="startTime();">
    <span id="time" style="color:#583B00;background-Color:yellow"></span>
    </body>
    </html>
    The date and time are of course those in the user's time zone and assume the accuracy of the clock in the user's computer.



    You can run both scripts with

    Code:
    <body onload="startTime(); areWeOpen()">
    A tiny detail - the hours in the 12-hour clock system have no 0 prefix. 13:30 = 01:30 PM.
    Last edited by Philip M; 05-05-2012 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.

  • Users who have thanked Philip M for this post:

    daleala (05-10-2012)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by iBall View Post
    btw, if this is for a real life application then this should really be done server side and not with javascript.
    Why? Unless it is that tired old nonsense about some people not having Javascript enabled in their browser. Which applies to any Javascript.

    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 Coder
    Join Date
    May 2012
    Location
    Australia
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks for all the awesome help so far guys, I'll look into the scripts you have suggested and see how they work.

  • #7
    New Coder
    Join Date
    May 2012
    Location
    Australia
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi Phillip.

    Thank-you for the OpenClosed script, it works brilliantly.
    Just a few questions.
    1. How do I get it to update every few seconds so when the "store" "opens", it updates from closed to open.
    I tried putting a loop with 5,000 cycles and a wait for 1 second between each loop, but I think I did it horribly wrong.
    2. Am I right in assuming that to code more than 1 "store", I simply change "function areWeOpen( ) {" to "function areWeOpen2( ) {" and "<body onload = "areWeOpen()">" to "<body onload = "areWeOpen2()">"

    Thank-you to everyone else who responded. It's encouraging to see everyone jumping in and being so helpful!

    In response to the Javascript question, it is better that it runs client side rather than server side as the machines it will be running on are all clones of each other and have JS enabled.



    Code:
    <html>
    <head>
    <style type="text/css">
    #openSign.OPEN {
        color: green;
        background-color: yellow;
        font-size: x-large;
    }
    #openSign.CLOSED {
        color : red;
        background-color: pink;
        font-size: large;
    }
    </style>
    
    <script type="text/javascript">
    
    var OPENAT = 11.5; // 7:30 AM ... change as appropriate - can be fractions of an hour e.g. 7.5 = 7:30am
    var CLOSEAT = 21; // 9:00 PM ... change as appropriate
    
    function areWeOpen( ) {
    var i=0;                                                                 // I added this to try to get it to "auto update"
    for (i=0;i<=500;i++)                                              // I also added this to try to get it to "auto update"
    {
    setTimeout( "end();", 100);                                     // I also added this to try to get it to "auto update"
    var sign = document.getElementById("openSign");
    var day = new Date().getDay();
    var hour = new Date().getHours();
    var mins = new Date().getMinutes();
    hour = hour + mins/60;
    if ( day >=1 && day <=5 && hour >= OPENAT && hour < CLOSEAT )  {
    sign.innerHTML = "We are now OPEN";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "Sorry, we are now CLOSED";
    sign.className = "CLOSED";
    
    }
    }
    }
    </script>
    </head>
    
    <body onload = "areWeOpen()">
    <center>
    <h2>Welcome to our Store</h2>
    <br/>
    <h3 id="openSign"></h3>
    <br/>
    </center>
    ... rest of page ...
    </body>
    </html>
    Last edited by daleala; 05-07-2012 at 08:40 AM.

  • #8
    New Coder
    Join Date
    May 2012
    Location
    Australia
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I realised I botched up point 2 in my assumptions when I tried it out for myself.

    Can there be multiple functions called up and ran when the page starts, while still allowing them to update and check the "open" or "closed" status every second?

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I have revised the script to take account of your requirements (two stores and updated every 30 seconds). Checking the time once every 30 seconds is quite enough in this situation as we are working with minutes. You can of course have as many separate stores as you require (just repeat the code in blue).

    You cannot have multiple scripts with the same function and/or variable names, so no, your assumption is not correct.


    Code:
    <html>
    <head>
    <style type="text/css">
    
    .OPEN {
        color: green;
        background-color: yellow;
        font-size: x-large;
        width:350px;
    }
    .CLOSED {
        color : red;
        background-color: pink;
        font-size: large;
        width:350px;
    }
    
    </style>
    
    <script type="text/javascript">
    
    var OPENAT1 = 7.5; // 7:30 AM ... change as appropriate - can be fractions of an hour e.g. 7.5 = 7:30am
    var CLOSEAT1 = 21; // 9:00 PM ... change as appropriate
    var OPENAT2 = 8.5;
    var CLOSEAT2  = 18;
    
    function areWeOpen( ) {
    
    var day = new Date().getDay();
    var hour = new Date().getHours();
    var mins = new Date().getMinutes();
    hour = hour + mins/60;
    
    var sign = document.getElementById("openSign1");
    if ( day >=1 && day <=5 && hour >= OPENAT1 && hour < CLOSEAT1 )  {  // Monday - Friday
    sign.innerHTML = "We are now OPEN";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "Sorry, we are now CLOSED";
    sign.className = "CLOSED";
    }
    
    var sign = document.getElementById("openSign2");
    if ( day >=1 && day <=5 && hour >= OPENAT2 && hour < CLOSEAT2 )  {  // Monday - Friday
    sign.innerHTML = "We are now OPEN";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "Sorry, we are now CLOSED";
    sign.className = "CLOSED";
    }
    
    setTimeout(areWeOpen,30000);  // update every 30 seconds
    }
    </script>
    </head>
    
    <body onload = "areWeOpen()">
    <center>
    <h2>Welcome to our Store</h2>
    <br/>
    <h3 id="openSign1"></h3>
    <br/>
    <h3 id="openSign2"></h3>
    <br/></center>
    ... rest of page ...
    </body>
    </html>
    If you prefer you can place the opening/closing times directly in each test:-

    if ( day >=1 && day <=5 && hour >= 7.5 && hour < 21 ) { // Monday - Friday
    Last edited by Philip M; 05-07-2012 at 01:09 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:

    daleala (05-10-2012)

  • #10
    New Coder
    Join Date
    May 2012
    Location
    Australia
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi guys.

    Thank you for your invaluable help so far. Because of your help, I have a code which I can actually use (insert happy smile here).
    At the moment it will show the daylight savings and non-daylight savings open/close times at the same time.
    Is there any way of taking daylight savings into consideration? Even if the dates have to be put in manually. The area that this will most likely be running is in a state which has daylight savings and the clock time changes by 1 hour twice a year.

    If this is not easy, is there any way of "hiding" or even "breaking" the DST code during non DST times so it won't show in the table, and vice versa? I'm happy to have an empty table during 1/2 of the year (as it will be an internal thing, the presentation isn't too important.)

    Here is the code so far (I haven't finished with the daylight savings calculations for each area, but some of them are done.)
    I know it's large and probably quite badly done, but at the end of the day it works, and that's the important thing.

    Code:
    <head>
    <title>Opening times for Victorian office</title>
     
    <style type="text/css">
     
    .OPEN {
        color: white;
        background-color: green;
        font-size: xx-small;
        width:60px;
    }
    .CLOSED {
        color : white;
        background-color: red;
        font-size: xx-small;
        width:60px;
    }
     
    </style>
     
    <script type="text/javascript">
     
    var OPENVIC     = 8.5; // 8:30 AM ... change as appropriate - can be fractions of an hour e.g. 7.5 = 7:30am
    var CLOSEVIC    = 17; // 5:00 PM ... change as appropriate
    var OPENNSW     = 8.5;
    var CLOSENSW    = 17;
    var OPENQLD     = 8.5;
    var CLOSEQLD    = 17;
    var OPENSA      = 9;
    var CLOSESA     = 17.5;
    var OPENWA      = 10.5;
    var CLOSEWA     = 19;
    var OPENTAS     = 8.5;
    var CLOSETAS    = 17;
    var OPENextra     = 10.5;
    var CLOSEextra    = 19;
     
    var OPENVICDST  = 8.5; // 7:30 AM ... change as appropriate - can be fractions of an hour e.g. 7.5 = 7:30am
    var CLOSEVICDST = 17; // 9:00 PM ... change as appropriate
    var OPENNSWDST  = 8.5;
    var CLOSENSWDST = 17;
    var OPENQLDDST  = 8.5;
    var CLOSEQLDDST = 17;
    var OPENSADST   = 9;
    var CLOSESADST  = 17.5;
    var OPENWADST   = 10.5;
    var CLOSEWADST  = 19;
    var OPENTASDST  = 8.5;
    var CLOSETASDST = 17;
    var OPENextraDST  = 10.5;
    var CLOSEextraDST = 19;
     
     
     
     
     
     
     
     
     
    function areWeOpen( ) {
     
    var day = new Date().getDay();
    var hour = new Date().getHours();
    var mins = new Date().getMinutes();
    hour = hour + mins/60;
     
    var sign = document.getElementById("openSignVIC");
    if ( day >=1 && day <=5 && hour >= OPENVIC && hour < CLOSEVIC )  {
    sign.innerHTML = "VIC is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "VIC is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignNSW");
    if ( day >=1 && day <=5 && hour >= OPENNSW && hour < CLOSENSW )  {
    sign.innerHTML = "NSW is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "NSW is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignQLD");
    if ( day >=1 && day <=5 && hour >= OPENQLD && hour < CLOSEQLD )  {
    sign.innerHTML = "QLD is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "QLD is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignSA");
    if ( day >=1 && day <=5 && hour >= OPENSA && hour < CLOSESA )  {
    sign.innerHTML = "SA is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "SA is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignWA");
    if ( day >=1 && day <=5 && hour >= OPENWA && hour < CLOSEWA )  {
    sign.innerHTML = "WA is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "WA is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignTAS");
    if ( day >=1 && day <=5 && hour >= OPENTAS && hour < CLOSETAS )  {
    sign.innerHTML = "TAS is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "TAS is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignextra");
    if ( day >=1 && day <=5 && hour >= OPENextra && hour < CLOSEextra )  {
    sign.innerHTML = "extra is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "extra is closed";
    sign.className = "CLOSED";
    }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    var sign = document.getElementById("openSignVICDST");
    if ( day >=1 && day <=5 && hour >= OPENVICDST && hour < CLOSEVICDST )  {
    sign.innerHTML = "VIC is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "VIC is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignNSWDST");
    if ( day >=1 && day <=5 && hour >= OPENNSWDST && hour < CLOSENSWDST )  {
    sign.innerHTML = "NSW is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "NSW is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignQLDDST");
    if ( day >=1 && day <=5 && hour >= OPENQLDDST && hour < CLOSEQLDDST )  {
    sign.innerHTML = "QLD is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "QLD is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignSADST");
    if ( day >=1 && day <=5 && hour >= OPENSADST && hour < CLOSESADST )  {
    sign.innerHTML = "SA is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "SA is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignWADST");
    if ( day >=1 && day <=5 && hour >= OPENWADST && hour < CLOSEWADST )  {
    sign.innerHTML = "WA is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "WA is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignTASDST");
    if ( day >=1 && day <=5 && hour >= OPENTASDST && hour < CLOSETASDST )  {
    sign.innerHTML = "TAS is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "TAS is closed";
    sign.className = "CLOSED";
    }
     
    var sign = document.getElementById("openSignextraDST");
    if ( day >=1 && day <=5 && hour >= OPENextraDST && hour < CLOSEextraDST )  {
    sign.innerHTML = "extra is open";
    sign.className = "OPEN";
    } 
    else {
    sign.innerHTML = "extra is closed";
    sign.className = "CLOSED";
    }
     
    setTimeout(areWeOpen,1000); // update every 1 second
    }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    function startVic() {
    var nowvic = new Date();
    var hvic=nowvic.getHours();
    var minvic=nowvic.getMinutes();
    var svic=nowvic.getSeconds();
    var ampmvic=(nowvic.getHours()>11)?"PM":"AM";
    if (hvic>12) {hvic-=12}
    if (hvic==0) {hvic=12};
    if (minvic<10) {minvic="0"+minvic}
    if (svic<10) {svic="0"+svic}
    document.getElementById("timevic").innerHTML=hvic+":"+minvic+":"+svic;
    var timvic = setTimeout("startVic()",1000);
    }
     
     
     
    function startNsw() {
    var nownsw = new Date();
    var hnsw=nownsw.getHours();
    var minnsw=nownsw.getMinutes();
    var snsw=nownsw.getSeconds();
    var ampmnsw=(nownsw.getHours()>11)?"PM":"AM";
    if (hnsw>12) {hnsw-=12}
    if (hnsw==0) {hnsw=12};
    if (minnsw<10) {minnsw="0"+minnsw}
    if (snsw<10) {snsw="0"+snsw}
    document.getElementById("timensw").innerHTML=hnsw+":"+minnsw+":"+snsw;
    var timnsw = setTimeout("startNsw()",1000);
    }
     
     
     
    function startQld() {
    var nowqld = new Date();
    var hqld=nowqld.getHours();
    var minqld=nowqld.getMinutes();
    var sqld=nowqld.getSeconds();
    var ampmqld=(nowqld.getHours()>11)?"PM":"AM";
    if (hqld>12) {hqld-=12}
    if (hqld==0) {hqld=12};
    if (minqld<10) {minqld="0"+minqld}
    if (sqld<10) {sqld="0"+sqld}
    document.getElementById("timeqld").innerHTML=hqld+":"+minqld+":"+sqld;
    var timqld = setTimeout("startQld()",1000);
    }
     
     
     
    function startTas() {
    var nowtas = new Date();
    var htas=nowtas.getHours();
    var mintas=nowtas.getMinutes();
    var stas=nowtas.getSeconds();
    var ampmtas=(nowtas.getHours()>11)?"PM":"AM";
    if (htas>12) {htas-=12}
    if (htas==0) {htas=12};
    if (mintas<10) {mintas="0"+mintas}
    if (stas<10) {stas="0"+stas}
    document.getElementById("timetas").innerHTML=htas+":"+mintas+":"+stas;
    var timtas = setTimeout("startTas()",1000);
    }
     
     
     
    function startSa() {
    var nowsa = new Date();
    var hsa=nowsa.getHours();
    var minsa=nowsa.getMinutes();
    var ssa=nowsa.getSeconds();
    var ampmsa=(nowsa.getHours()>11)?"PM":"AM";
    if (hsa>12) {hsa-=12}
    if (hsa==0) {hsa=12};
    if (minsa<10) {minsa="0"+minsa}
    if (ssa<10) {ssa="0"+ssa}
    if (minsa<30)
            {
                    minsa=minsa+30
                    hsa=hsa-1
            }
    else
            {
                    minsa=minsa-30
            }
    document.getElementById("timesa").innerHTML=hsa+":"+minsa+":"+ssa;
    var timsa = setTimeout("startSa()",1000);
    }
     
     
     
    function startWa() {
    var nowwa = new Date();
    var hwa=nowwa.getHours();
    var minwa=nowwa.getMinutes();
    var swa=nowwa.getSeconds();
    var ampmwa=(nowwa.getHours()>11)?"PM":"AM";
    if (hwa>12) {hwa-=12}
    if (hwa==0) {hwa=12};
    if (minwa<10) {minwa="0"+minwa}
    if (swa<10) {swa="0"+swa}
    if (hwa>2)
            {
                    hwa=hwa-2
     
            }
    else
    
            {
                    hwa=hwa+10
            }
    document.getElementById("timewa").innerHTML=hwa+":"+minwa+":"+swa;
    var timwa = setTimeout("startWa()",1000);
    }
     
     
     
    function startextra() {
    var nowextra = new Date();
    var hextra=nowextra.getHours();
    var minextra=nowextra.getMinutes();
    var sextra=nowextra.getSeconds();
    var ampmextra=(nowextra.getHours()>11)?"PM":"AM";
    if (hextra>12) {hextra-=12}
    if (hextra==0) {hextra=12};
    if (minextra<10) {minextra="0"+minextra}
    if (sextra<10) {sextra="0"+sextra}
    if (hextra>2)
            {
                    hextra=hextra-2
     
            }
    else
            {
                    hextra=hextra+10
            }
    document.getElementById("timeextra").innerHTML=hextra+":"+minextra+":"+sextra;
    var timextra = setTimeout("startextra()",1000);
    }
     
     
     
     
     
     
     
     
    function startVicdst() {
    var nowvicdst = new Date();
    var hvicdst=nowvicdst.getHours();
    var minvicdst=nowvicdst.getMinutes();
    var svicdst=nowvicdst.getSeconds();
    var ampmvicdst=(nowvicdst.getHours()>11)?"PM":"AM";
    if (hvicdst>12) {hvicdst-=12}
    if (hvicdst==0) {hvicdst=12};
    if (minvicdst<10) {minvicdst="0"+minvicdst}
    if (svicdst<10) {svicdst="0"+svicdst}
    document.getElementById("timevicdst").innerHTML=hvicdst+":"+minvicdst+":"+svicdst;
    var timvicdst = setTimeout("startVicdst()",1000);
    }
     
     
     
    function startNswdst() {
    var nownswdst = new Date();
    var hnswdst=nownswdst.getHours();
    var minnswdst=nownswdst.getMinutes();
    var snswdst=nownswdst.getSeconds();
    var ampmnswdst=(nownswdst.getHours()>11)?"PM":"AM";
    if (hnswdst>12) {hnswdst-=12}
    if (hnswdst==0) {hnswdst=12};
    if (minnswdst<10) {minnswdst="0"+minnswdst}
    if (snswdst<10) {snswdst="0"+snswdst}
    document.getElementById("timenswdst").innerHTML=hnswdst+":"+minnswdst+":"+snswdst;
    var timnswdst = setTimeout("startNswdst()",1000);
    }
     
     
     
    function startQlddst() {
    var nowqlddst = new Date();
    var hqlddst=nowqlddst.getHours();
    var minqlddst=nowqlddst.getMinutes();
    var sqlddst=nowqlddst.getSeconds();
    var ampmqlddst=(nowqlddst.getHours()>11)?"PM":"AM";
    if (hqlddst>12) {hqlddst-=12}
    if (hqlddst==0) {hqlddst=12};
    if (minqlddst<10) {minqlddst="0"+minqlddst}
    if (sqlddst<10) {sqlddst="0"+sqlddst}
    document.getElementById("timeqlddst").innerHTML=hqlddst+":"+minqlddst+":"+sqlddst;
    var timqlddst = setTimeout("startQlddst()",1000);
    }
     
     
     
    function startTasdst() {
    var nowtasdst = new Date();
    var htasdst=nowtasdst.getHours();
    var mintasdst=nowtasdst.getMinutes();
    var stasdst=nowtasdst.getSeconds();
    var ampmtasdst=(nowtasdst.getHours()>11)?"PM":"AM";
    if (htasdst>12) {htasdst-=12}
    if (htasdst==0) {htasdst=12};
    if (mintasdst<10) {mintasdst="0"+mintasdst}
    if (stasdst<10) {stasdst="0"+stasdst}
    document.getElementById("timetasdst").innerHTML=htasdst+":"+mintasdst+":"+stasdst;
    var timtasdst = setTimeout("startTasdst()",1000);
    }
     
     
     
    function startSadst() {
    var nowsadst = new Date();
    var hsadst=nowsadst.getHours();
    var minsadst=nowsadst.getMinutes();
    var ssadst=nowsadst.getSeconds();
    var ampmsadst=(nowsadst.getHours()>11)?"PM":"AM";
    if (hsadst>12) {hsadst-=12}
    if (hsadst==0) {hsadst=12};
    if (minsadst<10) {minsadst="0"+minsadst}
    if (ssadst<10) {ssadst="0"+ssadst}
    document.getElementById("timesadst").innerHTML=hsadst+":"+minsadst+":"+ssadst;
    var timsadst = setTimeout("startSadst()",1000);
    }
     
     
     
    function startWadst() {
    var nowwadst = new Date();
    var hwadst=nowwadst.getHours();
    var minwadst=nowwadst.getMinutes();
    var swadst=nowwadst.getSeconds();
    var ampmwadst=(nowwadst.getHours()>11)?"PM":"AM";
    if (hwadst>12) {hwadst-=12}
    if (hwadst==0) {hwadst=12};
    if (minwadst<10) {minwadst="0"+minwadst}
    if (swadst<10) {swadst="0"+swadst}
    if (hwadst>1)
            {
                    hwadst=hwadst-1
     
            }
    else
            {
                    hwadst=hwadst+11
            }
    document.getElementById("timewadst").innerHTML=hwadst+":"+minwadst+":"+swadst;
    var timwadst = setTimeout("startWadst()",1000);
    }
     
     
     
    function startextradst() {
    var nowextradst = new Date();
    var hextradst=nowextradst.getHours();
    var minextradst=nowextradst.getMinutes();
    var sextradst=nowextradst.getSeconds();
    var ampmextradst=(nowextradst.getHours()>11)?"PM":"AM";
    if (hextradst>12) {hextradst-=12}
    if (hextradst==0) {hextradst=12};
    if (minextradst<10) {minextradst="0"+minextradst}
    if (sextradst<10) {sextradst="0"+sextradst}
    if (hextradst>1)
            {
                    hextradst=hextradst-1
     
            }
    else
            {
                    hextradst=hextradst+11
            }
    document.getElementById("timeextradst").innerHTML=hextradst+":"+minextradst+":"+sextradst;
    var timextradst = setTimeout("startextradst()",1000);
    }
     
     
    </script>
     
     
     
     
     
    </head>
     
     
     
     
     
     
     
    <body onload="areWeOpen(); startVic(); startNsw(); startQld(); startTas(); startSa(); startWa(); startextra(); startVicdst(); startNswdst(); startQlddst(); startTasdst(); startSadst(); startWadst(); startextradst()">
     
     
     
     
    <center>
     
    <h5>Opening times - open or closed</h5>
     
     
     
    <h6>NON DAYLIGHT SAVINGS</h6>
    <table width="200" border="1">
      <tr>
        <td width="20%">GMT+10</td>
        <td width="50%"><span id="timevic">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignVIC">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+10</td>
        <td width="50%"><span id="timensw">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignNSW">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+10</td>
        <td width="50%"><span id="timeqld">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignQLD">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+10</td>
        <td width="50%"><span id="timetas">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignTAS">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+9.5 </td>
        <td width="50%"><span id="timesa">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignSA">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+8 </td>
        <td width="50%"><span id="timewa">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignWA">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+8 </td>
        <td width="50%"><span id="timeextra">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignextra">&nbsp;</h3></td>
      </tr>
    </table>
     
     
    <h6>DAYLIGHT SAVINGS</h6>
    <table width="200" border="1">
      <tr>
        <td width="20%">GMT+11</td>
        <td width="50%"><span id="timevicdst">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignVICDST">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+11</td>
        <td width="50%"><span id="timenswdst">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignNSWDST">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+10</td>
        <td width="50%"><span id="timeqlddst">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignQLDDST">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+11</td>
        <td width="50%"><span id="timetasdst">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignTASDST">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+10.5</td>
        <td width="50%"><span id="timesadst">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignSADST">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+8 </td>
        <td width="50%"><span id="timewadst">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignWADST">&nbsp;</h3></td>
      </tr>
      <tr>
        <td width="20%">GMT+8 </td>
        <td width="50%"><span id="timeextradst">&nbsp;</span></td>
        <td width="30%"><h3 id="openSignextraDST">&nbsp;</h3></td>
      </tr>
    </table>
     
    </center>
     
     
    </body>
    </html>

  • #11
    New Coder
    Join Date
    May 2012
    Location
    Australia
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Would working in UTC/GMT time be better?

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function startVic() {
    var nowvic = new Date();
    var hvic=nowvic.getUTCHours();
    var minvic=nowvic.getUTCMinutes();
    var svic=nowvic.getUTCSeconds();
    if (hvic>12) {hvic-=12}
    if (hvic==0) {hvic=12};
    if (hvic<=2)
    	{
    		hvic=hvic+10
    	}
    else
    	{
    		hvic=hvic-2
    	}
    if (minvic<10) {minvic="0"+minvic}
    if (svic<10) {svic="0"+svic}
    document.getElementById("timevic").innerHTML=hvic+":"+minvic+":"+svic;
    var timvic = setTimeout("startVic()",1000);
    }
    
    </script>
    </head>
    
    
    
    <body onload="startVic()">
     
    <span id="timevic"></span>
    </body>
    </html>
    If I have done this right, and I am hoping that I have, it will show the time in Victoria, regardless of which time zone it has been opened in (GMT+10 hours).
    Can this be revised to add a test for whether the daylight savings is on or not, and if it is daylight savings, to add 11 instead of 10 to the hours value?

    Thank-you so much for your help so far!

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    If you want to show the time in Victoria Australia regardless of the location of the user you must use UTC(GMT) time and add the appropriate number of hours (10).

    It is possible to detect whether a certain region uses DST, but as the dates of DST implementation vary from country to country it is not possible to detect whether or not DST is actually in operation in a certain country on any specific date.

    Assuming that all Australian states use the same DST dates then you can simply program in the actual dates (which must be known to you) into your script to add an additional hour.

    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.

  • #13
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Philip M View Post
    but as the dates of DST implementation vary from country to country it is not possible to detect whether or not DST is actually in operation in a certain country on any specific date.
    That statement is incorrect.

    JavaScript can determine whether a given date is on daylight saving time or not.

    First you get the 1st January and 1st July dates and check the timezone offsets on each date. If they are the same then that location doesn't use daylight saving. If they are different then it is easy to tell which offset is standard time and which dst because dst is always ahead.

    If you then get any other date of the year and compare its timezone offset to the ones you already have you can tell whether that date is on daylight saving time or not.

    The only thing it relies on is that the computer the browser is running on has the correct dates stored in it for when dst starts and finishes.

    The only complication is that as dst doesn't start and end at midnight the answer you get on the days dst stars and ends will depend on which side of the changeover time the actual time is set to.

    You can't tell whether a different computer is on DST or not but JavaScript can tell whether the computer it is running on thinks it is DST or not.
    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.

  • Users who have thanked felgall for this post:

    daleala (05-10-2012)

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by felgall View Post
    If you then get any other date of the year and compare its timezone offset to the ones you already have you can tell whether that date is on daylight saving time or not.
    Thank you for that information!

    Is this the correct logic?

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    var now =new Date();  // today
    var yr = now.getFullYear();
    var DSTused = false;
    
    var  jan = new Date(yr,0,1);  // 1st January
    var janoff = jan.getTimezoneOffset();
    var  jul = new Date(yr,6,1);  // 1st July
    var juloff = jul.getTimezoneOffset();
    if (janoff != juloff) {
    DSTused = true;
    alert ("This country uses DST");
    }
    else {
    alert ("This country does NOT use DST");
    }
    
    
    if (DSTused) {
    var maxval = Math.max(janoff, juloff);  // Note that JavaScript returns the offset in minutes and reverses the sign (so for example time zone +10 will return an offset of -600).
    var ntzo = now.getTimezoneOffset();
    if (ntzo != maxval) {
    alert ("DST is in operation on this day")
    }
    else {
    alert ("The country uses DST but DST is NOT in operation on this day");
    }
    }
    
    </script>
    Last edited by Philip M; 05-11-2012 at 07:42 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.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Ah well, at least I can tell the difference between the nominative and accusative pronouns, which semi-literate people often get confused.

    I said "It is possible to detect whether a certain region uses DST, but as the dates of DST implementation vary from country to country it is not possible to detect whether or not DST is actually in operation in a certain country on any specific date."

    By which I intended to convey:-

    It is possible to detect whether DST is used and is currently in operation in the user's own country, but AFAIK it is not possible for a user in one country to detect whether DST is currently in operation in some other country, e.g. Japan or Argentina. This is because the dates of implementation in those counties (assuming that they do use DST) vary.
    Last edited by Philip M; 05-10-2012 at 12:09 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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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