-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.

Deval Shah
  • 1,094
  • 8
  • 22
  • 1
    No. The [time zone](http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.7) is provided by the system, it is a read only property. However, you can generate values for any timezone you choose. – RobG May 31 '13 at 12:13
  • @DevalShah: I don't think jQuery UI datepicker doesn't offer such an option. Or do you use a different one? – Bergi May 31 '13 at 13:08

2 Answers2

1

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

3on
  • 6,291
  • 3
  • 26
  • 22
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.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • can we do with Override Javascript's Date property to give time specific to timezone?? @RobG – Deval Shah May 31 '13 at 12:28
  • Why don't you leave `getTimezoneOffset` away and just use the UTC methods? – Bergi May 31 '13 at 12:32
  • @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. – Bergi May 31 '13 at 12:33
  • 1
    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. – RobG May 31 '13 at 12:33
  • @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. – RobG May 31 '13 at 12:34
  • @RobG: No, `d.setUTCMinutes(d.getUTCMinutes() + offsetInMinutes); return [d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()].map(z).join(':')` would just work as well without doing any maths. – Bergi May 31 '13 at 12:37
  • @Bergi—fair enough. Seems my answer isn't what the OP wants anyway. C'est la vie. – RobG May 31 '13 at 13:12