Skip to content

Instantly share code, notes, and snippets.

@thebagg
Created December 18, 2013 07:00
Show Gist options
  • Save thebagg/8018352 to your computer and use it in GitHub Desktop.
Save thebagg/8018352 to your computer and use it in GitHub Desktop.
Get todays date in DD/MM/YYYY format.
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = dd+'/'+mm+'/'+yyyy;
document.write(today);
@DarckBlezzer
Copy link

DarckBlezzer commented Dec 3, 2021

// using padStart
 let date = new Date()
 let day = `${(date.getDate())}`.padStart(2,'0');
 let month = `${(date.getMonth()+1)}`.padStart(2,'0');
 let year = date.getFullYear();
 
 console.log(`${day}-${month}-${year}`)

@DarckBlezzer
Copy link

DarckBlezzer commented Dec 3, 2021

// using slice
let date = new Date()
let day = `0${date.getDate()}`.slice(-2); //("0"+date.getDate()).slice(-2);
let month = `0${date.getMonth()+1}`.slice(-2);
let year = date.getFullYear();

console.log(${day}-${month}-${year})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment