Skip to content

Instantly share code, notes, and snippets.

@kishorpawar
Created May 21, 2020 14:35
Show Gist options
  • Save kishorpawar/dbf20b45c0f4836a6e46f5ef7095cbe8 to your computer and use it in GitHub Desktop.
Save kishorpawar/dbf20b45c0f4836a6e46f5ef7095cbe8 to your computer and use it in GitHub Desktop.
// This function converts UTC date string to browser's time zone.
// and returns locale formatted date.
function toUserTimezone(utcDate){   
timezoneOffset = new Date().getTimezoneOffset() * -1;   
tojsDateObj = new Date(utcDate);   
convertedDate = new Date(tojsDateObj.getTime() + (timezoneOffset * 60000));   
return convertedDate.toLocaleString();
}
// this function coverts browser's zone specific dates to UTC timezone.
function toUTCTimezone(zoneDate){   
timezoneOffset = new Date().getTimezoneOffset() * -1;   
tojsDateObj = new Date(zoneDate);   
convertedDate = new Date(tojsDateObj.getTime() - (timezoneOffset * 60000));   
return convertedDate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment