Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X

UTC Server Date Timezone conversion on Browser

Got some issues converting date from the server to the timezone of the user viewing it. You can imagine displaying date and time from the server and the user can not see it interpreted in his/her own timezone. I solved this on a project and would like to share my solution so I can save someone from stress. Below is the javascript to use.


function timezoneConverter(mydate){
	var d = new Date(mydate);
	
	d.setUTCFullYear(d.getFullYear());
	d.setUTCMonth(d.getMonth());
	d.setUTCDate(d.getDate());
	d.setUTCHours(d.getHours());
	d.setUTCMinutes(d.getMinutes());
	d.setUTCSeconds(d.getSeconds());
	
	return d;
}

Make sure your date is a valid date in Java Script. You might like to format your date, you can use momentjs for that. Hope this helped someone.

 


comments powered by Disqus