Skip to content

Instantly share code, notes, and snippets.

@agrublev
Last active October 22, 2019 19:56
Show Gist options
  • Save agrublev/fe8a17740e187aa650c8802db5acf9b7 to your computer and use it in GitHub Desktop.
Save agrublev/fe8a17740e187aa650c8802db5acf9b7 to your computer and use it in GitHub Desktop.
Generate filename with date
/**
* Return a timestamp with the format "m-d-yy"
* @type {Date}
*/
function timeStamp() {
// Create a date object with the current time
let now = new Date();
// Create an array with the current month, day and time
let date = [now.getMonth() + 1, now.getDate(), now.getFullYear()];
// Return the formatted string
return date.join("-");
}
/**
* Return a timestamp with the format "m-d-yy_hh:MM:ss"
* @type {Date}
*/
function timeStampWithTime() {
// Create a date object with the current time
let now = new Date();
// Create an array with the current month, day and time
let date = [now.getMonth() + 1, now.getDate(), now.getFullYear()];
// Create an array with the current hour, minute and second
let time = [now.getHours(), now.getMinutes(), now.getSeconds()];
time = time.map(e => {
if (e < 10) {
return (e + "").padStart(2, "0");
} else {
return e;
}
});
// Return the formatted string
return date.join("-") + "_" + time.join("-");
}
function timeStampPretty() {
// Create a date object with the current time
let now = new Date();
/**
* SUFFIX GETTING
* @type {{month: string, year: string, day: string}}
*/
let options = { day: "numeric", year: "numeric", month: "long" };
let sfx = ["th", "st", "nd", "rd"];
let val = now.getDate() % 100;
let sffx = sfx[(val - 20) % 10] || sfx[val] || sfx[0];
let dddd = now.toLocaleDateString("en-US", options); // Saturday, September 17, 2016
let partsz = dddd.split(",");
let month = partsz[0].split(" ")[0];
// " " +
// partsz[0].split(" ")[1] +
// sffx +
// "," +
// partsz[1];
// Create an array with the current month, day and time
let date = [month, now.getDate() + sffx, now.getFullYear()];
// Create an array with the current hour, minute and second
let time = [now.getHours(), now.getMinutes(), now.getSeconds()];
time = time.map(e => {
if (e < 10) {
return (e + "").padStart(2, "0");
} else {
return e;
}
});
// Return the formatted string
return date.join(" ") + " " + time.join(":");
}
timeStampPretty(); //?
function twoDigitPad(num) {
return num < 10 ? "0" + num : num;
}
function timeStampFormat(patternStr = "M/d/yyyy", date = new Date()) {
let monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
let dayOfWeekNames = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
if (!patternStr) {
patternStr = "M/d/yyyy";
}
let day = date.getDate(),
month = date.getMonth(),
year = date.getFullYear(),
hour = date.getHours(),
minute = date.getMinutes(),
second = date.getSeconds(),
h = hour % 12,
hh = twoDigitPad(h),
HH = twoDigitPad(hour),
mm = twoDigitPad(minute),
suffix = (() => {
let sfx = ["th", "st", "nd", "rd"];
let val = date.getDate() % 100;
let sffx = sfx[(val - 20) % 10] || sfx[val] || sfx[0];
return sffx;
})();
(ss = twoDigitPad(second)),
(aaa = hour < 12 ? "AM" : "PM"),
(EEEE = dayOfWeekNames[date.getDay()]),
(EEE = EEEE.substr(0, 3)),
(D = twoDigitPad(day)),
(M = month + 1),
(MM = twoDigitPad(M)),
(MMMM = monthNames[month]),
(MMM = MMMM.substr(0, 3)),
(yyyy = year + ""),
(yy = yyyy.substr(2, 2));
// checks to see if month name will be used
patternStr = patternStr
.replace("hh", hh)
.replace("h", h)
.replace("HH", HH)
.replace("H", hour)
.replace("mm", mm)
.replace("m", minute)
.replace("ss", ss)
.replace("s", second)
.replace("Z", suffix)
.replace("D", D)
.replace("d", day)
.replace("EEEE", EEEE)
.replace("EEE", EEE)
.replace("yyyy", yyyy)
.replace("yy", yy)
.replace("aaa", aaa);
if (patternStr.indexOf("MMM") > -1) {
patternStr = patternStr.replace("MMMM", MMMM).replace("MMM", MMM);
} else {
patternStr = patternStr.replace("MM", MM).replace("M", M);
}
return patternStr;
}
console.log(timeStampFormat());
console.log(timeStampFormat("DZ-MMM-yyyy")); //OP's request
console.log(timeStampFormat("dZ-MMM-yyyy")); //OP's request
console.log(timeStampFormat("EEEE, MMMM Z, yyyy HH:mm:ss aaa"));
console.log(timeStampFormat("EEE, MMM dZ, yyyy HH:mm"));
console.log(timeStampFormat("yyyy-MM-dZ HH:mm:ss"));
console.log(timeStampFormat("M/d/yyyy HH:mm aaa"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment