To format a date in Typescript, one option is to use the Moment.js library.
First, install Moment.js via npm:
index.ts19 chars2 lines
Then, import moment at the beginning of your file:
index.ts29 chars2 lines
Assuming you have a date object named myDate
, you can format it as desired using the format()
function like this:
index.ts75 chars2 lines
Here, the format string "MMMM D, YYYY h:mm:ss.SSS A"
specifies the desired date format. Breakdown of the format string:
MMMM
: full month nameD
: day of the monthYYYY
: four-digit yearh
: hour (12-hour clock)mm
: minutess
: secondSSS
: millisecondA
: uppercase AM/PMNote that the above code assumes your myDate
object is of type Date
. If it is a string or some other format, you may need to use Moment.js's parse()
function to convert it to a Date
object first.
gistlibby LogSnag