format a date in to the format october 13, 2014 11:13:00 am in typescript

You can use either the built-in Date object in JavaScript or a library like moment.js to format a date in TypeScript. Here are examples using both methods:

Using the built-in Date object:

index.ts
const date = new Date('2014-10-13T11:13:00');
const formattedDate = `${date.toLocaleString('en-US', { month: 'long' })} ${date.getDate()}, ${date.getFullYear()} ${date.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true })}`;

console.log(formattedDate); // Output: October 13, 2014 11:13:00 AM
337 chars
5 lines

Using moment.js:

index.ts
import * as moment from 'moment';

const date = moment('2014-10-13T11:13:00');
const formattedDate = date.format('MMMM D, YYYY hh:mm:ss A');

console.log(formattedDate); // Output: October 13, 2014 11:13:00 AM
210 chars
7 lines

In the examples above, the date is first parsed from a string in ISO 8601 format ('YYYY-MM-DDThh:mm:ss') and then formatted using different methods. The format string for moment.js is similar to the one used by the strftime() function in C.

gistlibby LogSnag