Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't "set" the timezone offset, it's a read only property that is based on system settings.</p> <p>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).</p> <p>e.g. </p> <pre><code>// Provide offsetInMintes to add to UTC to get required time, // e.g. Nepal Standard Time is UTC +05:45 -&gt; +345 minutes function generateOffsetTime(offsetInMinutes) { function z(n){return (n&lt;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)); </code></pre> <h3>Edit</h3> <p>You could add your own methods to <em>Date.prototype</em>:</p> <pre><code>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&lt;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()); </code></pre> <p>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:</p> <pre><code>d.setOffset(600, 'AEST'); console.log(d.getOffsetFullDate() + ' ' + d.getOffsetTime()); d.setOffset(630, 'LHI'); console.log(d.getOffsetFullDate() + ' ' + d.getOffsetTime()); </code></pre> <p>But I still think it's better to build your own date constructor that leverages the built in Date rather than extends it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload