6

I'm currently trying to develop a countdown timer page. Here is the countdown timer code:

var clock;

$(document).ready(function() {

    // Grab the current date
    var currentDate = new Date();

    // Set some date in the future. In this case, it's always Jan 1
    var futureDate = new Date("July 01, 2015 22:00:00");

    // Calculate the difference in seconds between the future and current date
    var diff = futureDate.getTime() / 1000 - currentDate.getTime() / 1000;

    if(diff < 0){
        // Instantiate a countdown FlipClock
        clock = $('.clock').FlipClock(0, {
            clockFace: 'DailyCounter',
            countdown: true
        });
        $('.message').html('Lets Go!!');
        $('.Go').removeAttr("disabled");
        $( "div.first" ).replaceWith( "<i style='color:red'>Lets Go!</i>" );
    }
    else{
        // Instantiate a countdown FlipClock
        clock = $('.clock').FlipClock(diff, {
            clockFace: 'DailyCounter',
            countdown: true,
            callbacks: {
                stop: function() {
                    $('.message').html('Lets Go!!');
                    $('.Go').removeAttr("disabled");
                    $( "div.first" ).replaceWith( "<i style='color:red'>Lets Go!</i>" );
                }
            }
        });
    }

});

The problem is that the countdown time varies per timezone. For example, a user in Australia will have a three-hour-shorter countdown time than that of a user from Malaysia (GMT+8).

How can I standardize/set the initial countdown date's timezone to GMT+8 so that users in different timezones have the same countdown time?

rgajrawala
  • 2,148
  • 1
  • 22
  • 35
Marcus Tan
  • 407
  • 1
  • 9
  • 26
  • 1
    Possible duplicate of [How do you create a JavaScript Date object with a set timezone without using a string representation](http://stackoverflow.com/q/439630/). – rgajrawala Jul 01 '15 at 06:36
  • No, it does not help me to solve my problem. @usandfriends – Marcus Tan Jul 01 '15 at 06:49
  • I think it does? Check out [this answer](http://stackoverflow.com/q/439630/#16048201). `var d = new Date(); d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 /* convert to UTC */ + (/* GMT+8 */ 8) *60*60*1000 ); console.log('GMT+8 Time:', d);` You can check if the result is correct [here](http://wwp.greenwichmeantime.com/time-zone/gmt-plus-8/). – rgajrawala Jul 01 '15 at 07:03
  • @usandfriends i had try, but it won't work on mine...i wanted to UTC +8 for my code, how? – Marcus Tan Jul 01 '15 at 07:34
  • "It won't work", meaning it errors out or it gives an output that is different from the GMT+8 clock I linked? Please post the output of the code in my previous comment. – rgajrawala Jul 01 '15 at 07:36
  • @usandfriends Output that different from what i want, what if UTC +8? should i remain the 8 also? – Marcus Tan Jul 01 '15 at 07:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82053/discussion-between-usandfriends-and-marcus-tan). – rgajrawala Jul 01 '15 at 07:44

2 Answers2

3

Based on our discussion, this example code works to convert any timezone time into UTC+8 timezone time.

var d = new Date();
d.setTime(d.getTime() + d.getTimezoneOffset() * 60 * 1000 /* convert to UTC */ + (/* UTC+8 */ 8) * 60 * 60 * 1000);
console.log('UTC+8 Time:', d);

Here is a JSFiddle for reference.

Although the console output shows that the timezones of the date objects are not UTC+0800, their date values (year, month, date, etc...) have all been converted into UTC+0800 time.

It is just not possible to edit the actual timezone of date objects, but it is possible to edit their date values to reflect the new timezone, and that is what we are doing here.

Community
  • 1
  • 1
rgajrawala
  • 2,148
  • 1
  • 22
  • 35
  • I have just tried this, new Date() is UTC (so you can make it a standard) only if you're taking the current time. If you give it a datetime from a db for example it's gonna convert it to local time, and users are gonna see different results in different timezone locations. – Adry Feb 21 '18 at 12:37
0

GMT time is the same as UTC time. Use JavaScript setUTCDate() Method

Example

Set the day of the month, according to UTC:

var d = new Date();
d.setUTCDate(15);

The result of d will be:

Wed Jul 15 2015 10:47:28 GMT+0400 (Russian Standard Time)

katerinkadar
  • 25
  • 1
  • 8