Created
October 19, 2017 15:04
-
-
Save M-Yankov/136728f241b78418506a8dcd33f94d63 to your computer and use it in GitHub Desktop.
Fxing the UTC dates on client side
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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