Quick formatted {dd-mm-yy hh:mm} date output in JavaScript



No matter how many times I need do it I always end up in docs or stackoverflow.

And let's be honest date formatting in js is ... well not that simple.

This snippet is as straightforward as possible, but gets job done. I usually use it for folder naming or other minor stuff, where getting separate lib like Moment.js just for that seems like overkill.

const t = new Date();
const formattedDate =
t.getDate() +
'-' +
t.getMonth() +
'-' +
t.getFullYear().toString().replace(/^\d{2}/, '') +
'\s' +
t.getHours() +
':' +
t.getMinutes(); // output '18-11-19 17:37'

Of course if you want other date than "now" you need to pass it in the Date constructor.

But if you are in need of more advanced, flexible and dependency-free ...

package.json

{
// ...
"dependencies": {},
//..
}

library for handling time in js date-fns should do just perfect.

Recent Posts

Quick formatted {dd-mm-yy hh:mm} date output in JavaScript

Dependency free module to copy all files (sync) from directory in nodejs

Git repository as dependency in package.json.

Iterating through nested JavaScript object and extracting data.

Modules Cheatsheet

Ultimate slugify function

XPath cheatsheet

Vanilla JS XPath query function

Online code utilities

← Back to all posts