chobo2
chobo2

Reputation: 85785

Question about TimeZones and Jquery CountDown plugin

I am looking to use this plugin: http://keith-wood.name/countdown.html

but I need to use the TimeZone feature of it. So I was looking at the sample code

$('#sydneyCountdown').countdown({until: liftoffTime, timezone: +10});

so +10 is the TimeOffSet number. Now I need to make it so I can do a jquery get request and grab the TimeOffSet from the server(which gets the users time from the db and does TimeOffSet).

However it seems that C# TimeOffSet returns something like this "+02:00"(not this is just a random zone not the same as the one in the jquery example).

So it seems like all the C# TimeOffSet follow that format +/-xx:xx

So I don't understand why the jquery plugin is only 2 digts while the other one is 4 digits.

Can I know off safley the last 2 digits in the C# tomatch the plugin format?

Edit - would this work?

            // working on how to get offsetTime will be posted soon.
            string time = "-08:30";      
            string[] split = new string[] {":"};
            string[] splited = time.Split(split, StringSplitOptions.RemoveEmptyEntries);
            int hours = Convert.ToInt32(splited[0]);
            int mins = Convert.ToInt32(splited[1]);
            int totalMins = (hours * 60) + mins;

So just convert the hours to mins and then add the mins to it?

Edit - With offSetTime

   var info = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
    TimeSpan span  = info.BaseUtcOffset;
    string time = Convert.ToString(span);
    string[] split = new string[] {":"};
    string[] splited = time.Split(split, StringSplitOptions.RemoveEmptyEntries);
    int hours = Convert.ToInt32(splited[0]);
    int mins = Convert.ToInt32(splited[1]);
    int totalMins = (hours * 60) + mins;

Problem though OffSet only gives me the number not the +/- sign. So I don't know how to get it.

Edit -

Never mind I it makes sense that they don't add the plus sign since I was just testing one that would have a "-" sign and it is shown.

Upvotes: 0

Views: 565

Answers (1)

Ateş Göral
Ateş Göral

Reputation: 140060

The plugin says:

Cater for time zones with the timezone setting, which is set to the target time's offset from GMT, in either hours or minutes.

So, I think based on the magnitude of the timezone value, it treats it either as hours or minutes. You should convert the <sign><hh>:<mm> format to into number of minutes, to account for timezones that are not hour-aligned. Something like:

var tz = "-08:30";
var tz_tokens = /([+\-])0?(\d+):(\d+)/.exec(tz);
var tz_minutes = (tz_tokens[1] + 1) * (tz_tokens[2] * 60 + tz_tokens[3]);

// ..., timezone: tz_minutes, ...

Upvotes: 1

Related Questions