Skip to content

Instantly share code, notes, and snippets.

@M-Yankov
Created October 19, 2017 15:04
Show Gist options
  • Save M-Yankov/136728f241b78418506a8dcd33f94d63 to your computer and use it in GitHub Desktop.
Save M-Yankov/136728f241b78418506a8dcd33f94d63 to your computer and use it in GitHub Desktop.
Fxing the UTC dates on client side
/*
This script will work only on the client-side
*/
function toClientDate(dateText) {
if (!validateInput(dateText)) {
return '';
}
var date = new Date(dateText);
var offset = date.getTimezoneOffset();
var theLocalDate = new Date(date.setMinutes(date.getMinutes() - offset));
return theLocalDate.format('dd/MMM/yyyy HH:mm');
}
function validateInput(text) {
if (!text) {
return false;
}
var trimText = text.trim();
if (!trimText) {
return false;
}
return true;
}
// Usage - fix all dates returned from server
window.onload = function () {
var elementsToConvert = document.querySelectorAll('.convertDate');
for (var i = 0; i < elementsToConvert.length; i++) {
elementsToConvert[i].innerHTML = toClientDate(elementsToConvert[i].innerHTML);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment