Skip to content

Instantly share code, notes, and snippets.

@Parables
Last active August 16, 2020 06:28
Show Gist options
  • Save Parables/4d090b5c083ec6216f39a70fab71088a to your computer and use it in GitHub Desktop.
Save Parables/4d090b5c083ec6216f39a70fab71088a to your computer and use it in GitHub Desktop.
Format date using tokens without any 3rd party libraries
let days = [
{ s: 'Sun', l: 'Sunday' },
{ s: 'Mon', l: "Monday" },
{ s: 'Tue', l: 'Tuesday' },
{ s: 'Wed', l: 'Wednesday' },
{ s: 'Thu', l: 'Thursday' },
{ s: 'Fri', l: 'Friday' },
{ s: 'Sat', l: 'Saturday' }
];
let months = [
{ s: 'Jan', l: 'January' },
{ s: 'Feb', l: 'February' },
{ s: 'Mar', l: 'March' },
{ s: 'Apr', l: 'April' },
{ s: 'May', l: 'May' },
{ s: 'Jun', l: 'June' },
{ s: 'Jul', l: 'July' },
{ s: 'Aug', l: 'August' },
{ s: 'Sep', l: 'September' },
{ s: 'Oct', l: 'October' },
{ s: 'Nov', l: 'November' },
{ s: 'Dec', l: 'December' },
];
// Got from StackOverflow....
function getOrdinal(day = 0) {
let selector;
if (day <= 0) selector = 4;
else if ((day > 3 && day < 21) || day % 10 > 3) selector = 0;
else selector = day % 10;
return ['th', 'st', 'nd', 'rd', ''][selector];
};
// this is how you want to format your date using the accepted tokens
let formatPattern = "DW MMMM do, yyyy"
function formatDate( year=0, month= 0, day=0) {
// these are the accepted tokens
let formatTokens = ["dd", "d", "o", "DDDD", "DDD", "DW", "mm", "m", "MMMM", "MMM", "yyyy", "yy"]
formatTokens.forEach(t => {
switch (t) {
case "dd": // replace with day of the month in 2-digits
formatPattern = formatPattern.replace(/[dd]{2}/g, day < 10 ? `0${day}` : `${day}`)
break;
case "d": // replace with day of the month in 1-digit
formatPattern = formatPattern.replace(/[d]{1}/g, `${day}`)
break;
case "o": // replace with ordinal day of the month value
formatPattern = formatPattern.replace(/[o]{1}/g, getOrdinal(day))
break;
case "DDDD": // replace with the full name of the day of the Week
formatPattern = formatPattern.replace(/[DDDD]{4}/g, getValue(year, month, day, "DDDD"))
break;
case "DDD": // replace with the short name of the day of the week
formatPattern = formatPattern.replace(/[DDD]{3}/g, getValue(year, month, day, "DDD"))
break;
case "DW": // replace with the nth day of the the Week
formatPattern = formatPattern.replace(/[DW]{2}/g, getValue(year, month, day, "DW"))
break;
case "mm": // replace with the month of the year in 2 digits
formatPattern = formatPattern.replace(/[mm]{2}/g, month < 10 ? `0${month}` : `${month}`)
break;
case "m": // replace with the month of the year in 2 digits
formatPattern = formatPattern.replace(/[m]{1}/g, `${month}`)
break;
case "MMMM": // replace with the full name of the month
formatPattern = formatPattern.replace(/[MMMM]{4}/g, getValue(year, month, day, "MMMM"))
break;
case "MMM": // replace with the short name of the month
formatPattern = formatPattern.replace(/[MMM]{3}/g, getValue(year, month, day, "MMM"))
case "yyyy": // replace with the year in 4 digit
formatPattern = formatPattern.replace(/[yyyy]{4}/g, `${year}`)
break;
case "yy": // replace with the yeaar in 2 digits
formatPattern = formatPattern.replace(/[yy]{2}/g, `${year.toString().substring(2)}`)
break;
default:
}
})
console.log(formatPattern)
return formatPattern
}
function getValue(year= 0, month= 0, day=0, token = "DW") {
let dayIndex = new Date(year, month, day).getDay()
let monthIndex = new Date(year, month - 1, day).getMonth()
return token === "MMMM" ? months[monthIndex].l :
token === "MMM" ? months[monthIndex].s :
token === "DDDD" ? days[dayIndex].l :
token === "DDD" ? days[dayIndex].s : (dayIndex + 1).toString()
}
formatDate(2020, 8, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment