It seems that JavaScript's Date() function can only return local date and time. Is there anyway to get time for a specific time zone, e.g., GMT-9?
Combining @Esailija and @D3mon-1stVFW, I figured it out: you need to have two time zone offset, one for local time and one for destination time, here is the working code:
var today = new Date(); var localoffset = -(today.getTimezoneOffset()/60); var destoffset = -4; var offset = destoffset-localoffset; var d = new Date( new Date().getTime() + offset * 3600 * 1000)
An example is here: http://jsfiddle.net/BBzyN/3/
The getTimezoneOffset() method returns the time difference between Greenwich Mean Time (GMT) and local time, in minutes. For example, If your time zone is GMT+2, -120 will be returned. Note: This method is always used in conjunction with a Date object.
The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.
JavaScript's internal representation uses the “universal” UTC time but by the time the date/time is displayed, it has probably been localized per the timezone settings on the user's computer. And, indeed, that's the way JavaScript is set up to work.
JavaScript Date getTimezoneOffset() getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.
var offset = -8; new Date( new Date().getTime() + offset * 3600 * 1000).toUTCString().replace( / GMT$/, "" ) "Wed, 20 Jun 2012 08:55:20"
<script> var offset = -8; document.write( new Date( new Date().getTime() + offset * 3600 * 1000 ).toUTCString().replace( / GMT$/, "" ) ); </script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With