Skip to content

Instantly share code, notes, and snippets.

@ThomasTJdev
Created December 24, 2018 08:46
Show Gist options
  • Save ThomasTJdev/5d2cbaa12ff454e9a8b09e1b4ce3ef40 to your computer and use it in GitHub Desktop.
Save ThomasTJdev/5d2cbaa12ff454e9a8b09e1b4ce3ef40 to your computer and use it in GitHub Desktop.
/*
Custom date
_____________________*/
$(function () {
formatDate("epochToDateHourInit", "YYYY-MM-DD HH:mm", false);
formatDate("epochToDateShortInit", "DD MMM", false);
formatDate("epochToDateExcelInit", "YYYY-MM-DD", false);
formatDate("epochToDateInit", "YYYY MMM DD", true);
});
function formatDate(elementClass, dateFormat, toggleShow) {
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var d = "";
if (dateFormat == "DD MMM 'YY") {
$("." + elementClass).each(function () {
if ($(this).text().indexOf("-") == -1) {
d = new Date($(this).text() * 1000);
$(this).text(("0" + d.getDate()).slice(-2) + " " + monthNames[d.getMonth()] + " '" + d.getFullYear().toString().substr(-2));
}
});
} else if (dateFormat == "DD MMM 'YY - HH:mm") {
$("." + elementClass).each(function () {
if ($(this).text().indexOf("-") == -1) {
d = new Date($(this).text() * 1000);
$(this).text(("0" + d.getDate()).slice(-2) + " " + monthNames[d.getMonth()] + " '" + d.getFullYear().toString().substr(-2) + " - " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2));
}
});
} else if (dateFormat == "DD MMM YYYY HH:mm") {
$("." + elementClass).each(function () {
if ($(this).text().indexOf("-") == -1) {
d = new Date($(this).text() * 1000);
$(this).text(("0" + d.getDate()).slice(-2) + " " + monthNames[d.getMonth()] + " " + d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2));
}
});
} else if (dateFormat == "MMM DD") {
$("." + elementClass).each(function () {
if ($(this).text().indexOf("-") == -1) {
d = new Date($(this).text() * 1000);
$(this).text(monthNames[d.getMonth()] + " " + ("0" + d.getDate()).slice(-2));
}
});
} else if (dateFormat == "DD MMM") {
$("." + elementClass).each(function () {
if ($(this).text().indexOf("-") == -1) {
d = new Date($(this).text() * 1000);
$(this).text(("0" + d.getDate()).slice(-2) + " " + monthNames[d.getMonth()]);
}
});
} else if (dateFormat == "YYYY-MM-DD") {
$("." + elementClass).each(function () {
if ($(this).text().indexOf("-") == -1) {
d = new Date($(this).text() * 1000);
$(this).text(d.getFullYear() + "-" + ("0" + (d.getMonth() + 1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2));
}
});
} else if (dateFormat == "YYYY-MM-DD HH:mm") {
$("." + elementClass).each(function () {
if ($(this).text().indexOf("-") == -1) {
d = new Date($(this).text() * 1000);
$(this).text(d.getFullYear() + "-" + ("0" + (d.getMonth() + 1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2) + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2));
}
});
} else if (dateFormat == "DD-MM-YYYY HH:mm") {
$("." + elementClass).each(function () {
if ($(this).text().indexOf("-") == -1) {
d = new Date($(this).text() * 1000);
$(this).text(("0" + d.getDate()).slice(-2) + "-" + ("0" + (d.getMonth() + 1)).slice(-2) + "-" + d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2));
}
});
} else if (dateFormat == "YYYY MMM DD") {
$("." + elementClass).each(function () {
if ($(this).text().indexOf("-") == -1) {
d = new Date($(this).text() * 1000);
$(this).text(d.getFullYear() + " " + monthNames[d.getMonth()] + " " + ("0" + d.getDate()).slice(-2));
}
});
} else {
console.log("Error: Wrong date formatting option specified.");
}
if (toggleShow) {
$("." + elementClass).show(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment