How to get formatted date in JavaScript?
When we print a date most of the cases we need custom formatting of the date. In many cases, this also needs some locale format as well. Different countries have different ways of presenting dates. The following are some ways of formatting dates in JavaScript.
Format date in Swedish
The date and the year is the numerical number most of the cases. But months are different in different languages. Following is the function to print the formatted date in Swedish:
let getFormattedDate = (timestamp) => { if(!timestamp) return "N/A"; var date = new Date(timestamp); var months = ['Jan','Febr','Mars','April','Maj','Juni','Juli','Aug','Sept','Okt','Nov','Dec']; var year = date.getFullYear(); var month = months[date.getMonth()]; var day = date.getDate(); return day + ' ' + month + ', ' + year; };
Usage of the function
getFormattedDate(Date.now())
Output
6 Maj, 2020