Last active
October 19, 2022 08:10
-
-
Save balsimpson/fe1b8975c22cf3e7825e245acd428f40 to your computer and use it in GitHub Desktop.
A simple function to convert a date to a human readable string
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
| // format date with time and AM/PM | |
| /** | |
| * A simple function to convert a date to a human readable string | |
| * @param date a date object or a string that can be parsed into a date object | |
| * @returns string formatted as "D MMM YYYY, hh:mm am/pm" | |
| */ | |
| const getFormattedDate = (date) => { | |
| return date.toLocaleString('en-GB', { day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: 'numeric', hour12: true }) | |
| } | |
| let d = getFormattedDate(new Date()); | |
| let nd = getFormattedDate("11 Oct 2022, 7:52 pm"); | |
| console.log(nd); // 11 Oct 2022, 7:52 pm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment