Skip to content

Instantly share code, notes, and snippets.

@taylormitchell
Last active August 31, 2021 19:52
Show Gist options
  • Select an option

  • Save taylormitchell/18dc83657c5f8e521462e74e6ca14429 to your computer and use it in GitHub Desktop.

Select an option

Save taylormitchell/18dc83657c5f8e521462e74e6ca14429 to your computer and use it in GitHub Desktop.
Javascript function to create a date string in Roam format
/**
* Returns the corresponding date suffix
* @param {number} d Day of the month
* @returns Date suffix
*/
function getDateSuffix(d) {
lastDigit = d % 10
if (d >= 11 && d <= 13) {
return "th"
} else if (lastDigit === 1) {
return "st"
} else if (lastDigit === 2) {
return "nd"
} else if (lastDigit === 3) {
return "rd"
} else {
return "th"
}
}
/**
* Return date string in Roam format
* @param {object} d Date object
* @returns Date string in Roam format
*/
function getRoamDate(d = new Date()) {
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
month = monthNames[d.getMonth()]
day = d.getDate()
suffix = getDateSuffix(day)
year = d.getFullYear()
return `${month} ${day}${suffix}, ${year}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment