How to format JS dates like PHP

I really like PHP's simple date() format options. For example date('Y-m-d').

I replicated most of this in a Typescript package, and published it on NPM.

See it here


Use the same format strings as you can use in PHP, but in Javascript.

Added in a small personal project, I added it up on Github as maybe someone else will find it useful. There are probably better solutions with more thought gone into them.

A lot of the timezone related formatting is not supported. Some functions copied from Stackoverflow (with credit url)

Examples

const date =  new Date("2020-10-01");

console.log(dateFormatter('d-m-Y', date));
// 01-10-2020

For more examples, see the test file.

Comments How to format JS dates like PHP