-2

i am making a ajax/jquery online app and I need a bit of help, i am storing the current day, month and year being used as global varibles, when the program starts at first the dates will be set to today's date, so my questions are pretty simple and are as follows:

''

3 simple j Query questions:

1) how do i get the current day in this format: 00

2) how do i get the current month in this format: 00

3) how do i get the current year in this format: 0000

Alex Winter
  • 37
  • 1
  • 1
  • 7

2 Answers2

5

Try this: Today's date

    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;
    console.log(today);
Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
1

Try this

var fixDate = function (date){
   return date<10?"0"+date: date;
}

var dateStr = new Date();

console.log(fixDate(dateStr.getDate())+"-"+fixDate(dateStr.getMonth()+1)+"-" + dateStr.getFullYear());
Anton
  • 32,245
  • 5
  • 44
  • 54