format date to dd.mm.yyyy in javascript

You can format a date in JavaScript to dd.mm.yyyy format by following these steps:

  1. Create a new Date object.
  2. Get the day, month, and year of the date object.
  3. Use string concatenation to combine these values in the desired format.

Here's the code:

index.tsx
let date = new Date();
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
if(month < 10) {
  month = '0' + month;
}
if(day < 10) {
  day = '0' + day;
}
let formattedDate = day + '.' + month + '.' + year;
console.log(formattedDate);
271 chars
13 lines

This code retrieves the current date, formats its parts into dd.mm.yyyy, and assigns the formatted date to the formattedDate variable. You can then use this variable however you need.

gistlibby LogSnag