开发者

How to set timezone in jquery countdown timer

开发者 https://www.devze.com 2022-12-30 18:49 出处:网络
I want to set GMT+5:30 as my timezone in jquery countdown. Start Time for countdown is \'Thu May 20 16:00:00 IST 2010\'

I want to set GMT+5:30 as my timezone in jquery countdown. Start Time for countdown is 'Thu May 20 16:00:00 IST 2010' End Time is 'Thu May 20 17:00:00 IST 2010' as value.

+330 is my timezone given in minutes. But my countdown starts from 00:35:00. I would have expected the countdown to start from 01:00:00 Not sure why this is discrepancy is there.

    <script t开发者_如何学Goype="text/javascript">
        $(function () {
            var endTime = '#{myBean.getCountDownDate()}';
            $('#defaultCountdown').countdown({
                until: endTime, format: 'HMS',
                timezone: +330,
                compact: true, description: '#{myBean.getCountDownDate()}'});
        });
    </script>


When using the until parameter the countdown plugin counts down until that time.

This will run for one hour using the correct offset.

$('#countdown').countdown({
    until: $.countdown.UTCDate(+330, 2010, 6-1, 20, 17),
    format: 'HMS',
    compact: true
});

Since 2010:06:20:17 has already passed it will display 00:00:00.

I would bet the reason you got 00:35:00 in your countdown is that you were looking at it around 2010:06:20:16:25.


What happens when you change your End Time format to 'Thu, 20 May 2010 17:00:00 IST'?

-edit-

It looks like you're not supposed to pass the date value to until as a String. You can pass in a Date to specify the exact date/time, but a string is only supposed to be used as a time offset, which is why you always get the same amount of time remaining when you refresh.

I couldn't get Date to convert the string with the 'IST' time zone, so I ended up using 'GMT+05:30'. I also put the timezone offset in terms of hours instead of minutes.

<script type="text/javascript">
$(function () {
    var endTime = "Tue, 29 Jun 2010 12:00:00 GMT+0530";
    $('#defaultCountdown').countdown({
        until: new Date(endTime), format: 'HMS',
        timezone: +5.5,
        compact: true, description: endTime.toString()});
});
</script>


to set timer with eastern timezone

<script>
    setInterval(function(){
        var timeZone = "America/New_York";
        var TimerEndDate = "Nov 25 2022";
        var endHour = 23;
        var endMinute = 59;
        var endSeconds = 59;
    
        //for starting time for timer 
        //bydefault set to america current time
        var nowDate = new Date();
        var nowTimeZone = convertTZ(nowDate, timeZone); 
        var now = new Date(nowTimeZone.getFullYear(),nowTimeZone.getMonth(),nowTimeZone.getDate());
        now.setHours(nowTimeZone.getHours(), nowTimeZone.getMinutes(), nowTimeZone.getSeconds());
    
        var endDate = new Date(TimerEndDate+" "+endHour+":"+endMinute+":"+endSeconds);
        var endDateTime = convertTZ(endDate, timeZone);
        var future = new Date(endDateTime.getFullYear(),endDateTime.getMonth(),endDateTime.getDate());
        future.setHours(endHour, endMinute, endSeconds);
    
        var difference = Math.floor((future - now) / 1000);
    
        var seconds = fixIntegers(difference % 60);
        difference = Math.floor(difference / 60);
    
        var minutes = fixIntegers(difference % 60);
        difference = Math.floor(difference / 60);
    
        var hours = fixIntegers(difference % 24);
        difference = Math.floor(difference / 24);
    
        var days = difference;
        $("#seconds").text(seconds + "s");
        $("#minutes").text(minutes + "m");
        $("#hours").text(hours + "h");
        $("#days").text(days + "d");
    }, 1000);
    function convertTZ(date, tzString) {
        return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString}));   
    }
    function fixIntegers(integer)
    {
        if (integer < 0)
            integer = 0;
        if (integer < 10)
            return "0" + integer;
        return "" + integer;
    }
    </script>
    <span id="days"></span>
    <span id="hours"></span>
    <span id="minutes"></span>
    <span id="seconds"></span>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号