-1

How can I force Browser to display all date objects to use Specific Timezone.Like passing Europe/London.Is there any way to do so??

Update

Here I want is that all Jquery Datepicker open date and time as per Specific Timezone instead of Client's Machine.

2
  • 1
    No. The time zone is provided by the system, it is a read only property. However, you can generate values for any timezone you choose. Commented May 31, 2013 at 12:13
  • @DevalShah: I don't think jQuery UI datepicker doesn't offer such an option. Or do you use a different one? Commented May 31, 2013 at 13:08

2 Answers 2

1

You can't "set" the timezone offset, it's a read only property that is based on system settings.

You can generate time and date values for any timezone offset by simply adding the client timezone offset, then adding whatever offset you want (note that javascript Date object's timezone offset has an opposite sense to the usual value, so you add both rather than subtracting one and adding the other).

e.g.

// Provide offsetInMintes to add to UTC to get required time,
// e.g. Nepal Standard Time is UTC +05:45 -> +345 minutes
function generateOffsetTime(offsetInMinutes) {
  function z(n){return (n<10? '0' : '') + n;}
  var d = new Date();
  d.setMinutes(d.getMinutes() + d.getTimezoneOffset() + offsetInMinutes);
  return [z(d.getHours()),z(d.getMinutes()),z(d.getSeconds())].join(':');
}

alert('The time in Nepal is ' + generateOffsetTime(345));

Edit

You could add your own methods to Date.prototype:

Date.prototype.setOffset = function(offsetInMinutes, offsetName) {
  this._offsetInMinutes = offsetInMinutes;
  this._offsetName = offsetName;
};

Date.prototype.getOffsetFullDate = (function() {
  var months = ('January February March April May June July ' +
               'August September October November December').split(' ');
  var days = 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split(' ');

  return function() {
    var d = new Date(+this);
    d.setMinutes(d.getMinutes() + d.getTimezoneOffset() + this._offsetInMinutes);
    return days[d.getDay()] + ' ' + d.getDate() + ' ' + months[d.getMonth()] + 
           ', ' + this.getFullYear();  
  }

}());

Date.prototype.getOffsetTime = (function() {
  function z(n){return (n<10? '0' : '') + n;}

  return function() {
    var d = new Date(+this);
    d.setMinutes(d.getMinutes() + d.getTimezoneOffset() + this._offsetInMinutes);
    return z(d.getHours()) + ':' + z(d.getMinutes()) + ':' +
           z(d.getSeconds()) + ' ' + this._offsetName;  
  }  
}());

var d = new Date();
d.setOffset(345, 'NST')
console.log(d.getOffsetFullDate() + ' ' + d.getOffsetTime());

Note that this keeps the date object's original timevalue, it adjusts values as they are retrieved so the timezone can be changed to get different values for the same date object, so you could continue with:

d.setOffset(600, 'AEST');
console.log(d.getOffsetFullDate() + ' ' + d.getOffsetTime());

d.setOffset(630, 'LHI');
console.log(d.getOffsetFullDate() + ' ' + d.getOffsetTime());

But I still think it's better to build your own date constructor that leverages the built in Date rather than extends it.

Sign up to request clarification or add additional context in comments.

7 Comments

can we do with Override Javascript's Date property to give time specific to timezone?? @RobG
Why don't you leave getTimezoneOffset away and just use the UTC methods?
@DevalShah: No, we cannot (first sentence of this answer). And if you meant overwriting the global Date object with our own implementation - we should not.
I suppose you can replace the built-in Date.prototype.toString to do whatever you want, but I don't think that's a good idea.
@Bergi—because then you'd need to do the maths of adding the offset yourself rather than letting the Date object do it for you.
|
1

Moment.js is a great library which could help you to achieve this.

3 Comments

Can you show an example? I didn't knew it could handle timezones as Europe/London
See my update in the dup link regarding moment-timezone.
my mistake moment just give you local and utc. Sorry I was wrong, there is a plugin for it but not sure what it is worth: github.com/moment/moment-timezone

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.