Last active
August 31, 2021 19:52
-
-
Save taylormitchell/18dc83657c5f8e521462e74e6ca14429 to your computer and use it in GitHub Desktop.
Javascript function to create a date string in Roam format
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
| /** | |
| * 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