In JavaScript, Date() object get time from your local system. And what, when you deploy your website to server and your server is in the Germany then it get time according to the Germany time zone And you want Indian time whatever your server location.
I recently face this situation. I am working on a website which runs only 8PM to 4AM according to the Indian time zone. So I found a solution for this on the internet which i am writing here.
before that you have to understand the offset, In JavaScript we can get offset by getTimezoneOffset() method of Date object and it gives the difference between UTC time and your local time.
For Example
Your server has Singapore time zone and you want indian time.
var d = new Date();
var time = d.getTime(); // time in milliseconds
var offset = d. getTimezoneOffset(); // gives -480
This is the offset of Singapore and tells that Singapore is ahead of UTC time by 480 minutes
-(minus) specify ahead of UTC time and +(plus) specify behind of UTC time.
Like, if UTC time is 1 PM then Singapore time is 9 PM
var offset = offset * 60 * 1000;
So for getting UTC time
var UTC = time + offset;
This is the actual UTC time which is located in London at Greenwich from where all other country time is calculated.
Now we have UTC time, for getting Indian time we have to know the offset of the India. and Indian offset is +5.5 hours, it means india is 5.5 hours ahaed of UTC time, thus by adding this to UTC time we can get the Indian time
var indianOffset = 5.5 * 60 * 60 * 1000; // in milliseconds
var IndianTime = UTC + indianOffset;
var nd = new Date(IndianTime);
// this nd is indian time.
Resource
http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/
For Example
Your server has Singapore time zone and you want indian time.
var d = new Date();
var time = d.getTime(); // time in milliseconds
var offset = d. getTimezoneOffset(); // gives -480
This is the offset of Singapore and tells that Singapore is ahead of UTC time by 480 minutes
-(minus) specify ahead of UTC time and +(plus) specify behind of UTC time.
Like, if UTC time is 1 PM then Singapore time is 9 PM
var offset = offset * 60 * 1000;
So for getting UTC time
var UTC = time + offset;
This is the actual UTC time which is located in London at Greenwich from where all other country time is calculated.
Now we have UTC time, for getting Indian time we have to know the offset of the India. and Indian offset is +5.5 hours, it means india is 5.5 hours ahaed of UTC time, thus by adding this to UTC time we can get the Indian time
var indianOffset = 5.5 * 60 * 60 * 1000; // in milliseconds
var IndianTime = UTC + indianOffset;
var nd = new Date(IndianTime);
// this nd is indian time.
Resource
http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/
Just using JavaScript's getTimezoneOffset() method isn't sufficient in always getting the correct offset of a location from UTC due to Daylight Saving's Time. To get the later you can tap into a Time Zone API such as Google TimeZone API. Take a look at this tutorial for reliably getting and displaying the local time of a city: http://www.javascriptkit.com/dhtmltutors/local-time-google-time-zone-api.shtml
ReplyDelete