0

I want to show the time from the timezone chosen by me on my webpage. I found the way to show the current time as it's presented here http://www.webestools.com/ftp/ybouane/scripts_tutorials/javascript/date_time/date_time.html :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Display Date and Time in Javascript</title>
        <script type="text/javascript" src="date_time.js"></script>
    </head>
    <body>
            <span id="date_time"></span>
            <script type="text/javascript">window.onload = date_time('date_time');</script>
    </body>
</html>

and JS:

function date_time(id)
{
        date = new Date;
        year = date.getFullYear();
        month = date.getMonth();
        months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'Jully', 'August', 'September', 'October', 'November', 'December');
        d = date.getDate();
        day = date.getDay();
        days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
        h = date.getHours();
        if(h<10)
        {
                h = "0"+h;
        }
        m = date.getMinutes();
        if(m<10)
        {
                m = "0"+m;
        }
        s = date.getSeconds();
        if(s<10)
        {
                s = "0"+s;
        }
        result = ''+days[day]+' '+months[month]+' '+d+' '+year+' '+h+':'+m+':'+s;
        document.getElementById(id).innerHTML = result;
        setTimeout('date_time("'+id+'");','1000');
        return true;
}

but if I understand it correctly - it always shows the current time from the timezone of the viewer of the page. So when someone logs in there from e.g. Australia - he will see his result, and the guy from Poland will see sth completely different. I would like to present on the webpage the current time in fixed timezone, let's say e.g. in New York. How should I modify this script to get the expected results? Thanks!

Mike
  • 23,542
  • 14
  • 76
  • 87
randomuser1
  • 2,733
  • 6
  • 32
  • 68
  • Removing php tag because this has nothing to do with php – Mike Apr 25 '15 at 22:10
  • possible duplicate of [How to initialize javascript date to a particular timezone](http://stackoverflow.com/questions/15141762/how-to-initialize-javascript-date-to-a-particular-timezone) – Mike Apr 25 '15 at 22:11
  • 1
    Take a look to moment.js : http://momentjs.com/timezone/ – Vincent Decaux Apr 25 '15 at 22:12
  • if you need one time source for any clients .. you need to use server-side time and present it via js. Like this http://www.javascriptkit.com/script/script2/servertime.shtml – daremachine Apr 25 '15 at 22:15
  • possible duplicate of [Time zone PHP refresh](http://stackoverflow.com/questions/29754334/time-zone-php-refresh) – Matt Johnson-Pint Apr 26 '15 at 00:04

1 Answers1

0

You can use moment-timezone to convert time to differnt timezone

Here Example:

function toTimeZone(time, zone) {
    var format = 'YYYY/MM/DD HH:mm:ss ZZ';
    return moment(time, format).tz(zone).format(format);
}
Karan Patyal
  • 371
  • 3
  • 8