Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change the Javascript's getTimezoneOffset to return a different time zone offset?

Tags:

javascript

Is there a way to change the Javascript's getTimezoneOffset to return a different time zone offset?

Fox example,my local time zone is GMT-8,but i want the Date.getTimezoneOffset() to return GMT-1,so i do this as follow:

var timeZone1 = new Date(Date.parse("2011-01-01T00:00:00-01:00"));
document.write("The local time zone is: GMT " + timeZone1.getTimezoneOffset()/60);
//show:The local time zone is: GMT-8

so,How to make it shows GMT-1?

Refference links:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

like image 306
zhkzyth Avatar asked Sep 03 '26 13:09

zhkzyth


2 Answers

I don't think javascript has a direct way to do this, soooo you can either calculate it yourself or use a date library.

http://code.google.com/p/datejs/ datejs is a pretty nice library.

To calculate yourself try,

function calcTime(offset) {
    d = new Date();

    //Deal with dates in milliseconds for most accuracy
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    newDateWithOffset = new Date(utc + (3600000*offset));

    //This will return the date with the locale format (string), or just return newDateWithOffset
    //and go from there.
    return newDateWithOffset.toLocaleString();

}

Then pass the offset as like +1 or -5, etc.. as in calcTime('-1')

like image 130
Matt Avatar answered Sep 06 '26 03:09

Matt


Quick and fast solution using pure JavaScript.

var currentTime = new Date();
var currentTimezone = currentTime.getTimezoneOffset();
    currentTimezone = (currentTimezone/60) * -1;

if (currentTimezone !== 0)
{
  utc = currentTimezone > 0 ? ' +' : ' ';
  utc += currentTimezone
}

alert(utc);
like image 35
tfont Avatar answered Sep 06 '26 03:09

tfont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!