Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Regarding date-time clock ...

  1. #11
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Ok, this problem is actually much simpler than I realized at first. All that you need to do is use the original version of this Local Time script that we've been talking about. Adding just one variable and perhaps doing a little trial and error math will get you what you are looking for. Add this variable (highlighted red) near the top of the script:

    Code:
    /***********************************************
    * Local Time script- © Dynamic Drive (http://www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit http://www.dynamicdrive.com/ for this script and 100s more.
    ***********************************************/
    
    var weekdaystxt=["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]
    
    var localOffset=new Date().getTimezoneOffset()*-1
    And use this method to call the script from the body:

    Code:
    Current Local Time: <span id="timecontainer"></span>
    
    <script type="text/javascript">
    new showLocalTime("timecontainer", "server-php", localOffset, "long")
    </script>
    Now, in many cases your server time is already GMT or may be fetched that way by this script. If so, the local time displayed will be correct and you are done. If it is not, that is where a little trial and error comes in. Determine the hours difference between the time displayed and your true local time, multiply by 60 and either add or subtract it in the call. For example, let's say that the time displayed is 3 hours later than it should be. 3 times 60 is 180, so you need to subtract it from the local offset:

    Code:
    new showLocalTime("timecontainer", "server-php", [localOffset-180], "long")
    That's it! Once you have the adjustment worked out for your locality it will be correct in any time zone. If you need help formatting the output (like before) once you have this working, let me know.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #12
    Join Date
    Dec 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hello John,

    Thanks. It appears that you may have solved the problem but I can't say for sure, as I don't know exactly where my server is.

    When I ran the script with no changes, the time was 6 hours early. As my server is in the USA, I guess that was gmt time but I'm not sure. Anyhow, I added 360 to localOffset and it now gives the correct time in Michigan.

    Here is the test page:

    URL removed as requested.

    Now, how can I tell if it will give the correct time for a viewer in say California, Alaska, or Russia, etc?

    If you can definitively answer that, you have the best date-time clock script available!

    Also, some minor tweaks:

    I don't know how to change the format of the script without rendering it inoperable.

    Here’s what it looks like now:

    Current Local Time: Saturday, December 24, 2005 07:14:35

    Here’s how I want it to look:

    Saturday, December 24, 2005
    Current time: 7:14:35 a.m.

    Note that I removed the leading “0” in front of the hour and added the “a.m.” in lower case.

    How do I change the script to achieve this?

    Lowell
    Last edited by ddadmin; 03-05-2008 at 09:42 PM.

  3. #13
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I tested this out on a friends server in CA. I am in PA and that worked. Also, your page shows the correct local time here in PA. That's the best 'proof' I can offer from here. Now about formatting, replace the showLocalTime.prototype.updateContainer function with this one:

    Code:
    showLocalTime.prototype.updateContainer=function(){
    var thisobj=this
    this.container.innerHTML=this.localtime.toLocaleDateString()+'<br>'
    var hour=this.localtime.getHours()
    var minutes=this.localtime.getMinutes()
    var seconds=this.localtime.getSeconds()
    var ampm=(hour>=12)? "p.m." : "a.m."
    var dayofweek=weekdaystxt[this.localtime.getDay()]
    this.container.innerHTML+="Current Time: "+formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm
    setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second
    }
    and change this (in the body):

    HTML Code:
    Current Local Time: <span id="timecontainer"></span>
    to this:

    HTML Code:
    <span id="timecontainer"></span>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #14
    Join Date
    Dec 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Eureka ...

    Hello John,

    Superb!!!

    You can see the results here,
    on my actual www home page:

    URL removed as requested.

    It's at the bottom:

    "Thanks for visiting our www site:"
    .........
    .........

    Now that you have this script, why not replace the other date-clock scripts with it, as it is now the most accurate way to do it or so we hope, until proven otherwise.

    Again, many thanks and if you are ever in Detroit,
    I'll be happy to buy you a beer!

    Lowell
    Last edited by ddadmin; 03-05-2008 at 09:42 PM.

  5. #15
    Join Date
    Feb 2006
    Location
    Warrington, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Can I just jump in here please?

    I have a related problem regarding displaying GMT/UTC on my aviation related website (which always times everything worldwide in GMT)?

    I have run the scripts in this thread and get no response or display for either date or time just two lines like this:-
    NaN
    Current Time: Na: Na: Na AM

    Using my PCs clock is OK but is not dynamic enough for me or a worldwide audience!!

    Any assistance would be greatlfully received

    My ISP here in the UK is www.ntlworld.com

Bookmarks

Posting Permissions

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