-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
  • 7
  • 3
    Perhaps google could help you rather than asking people to give you code. It's very easy to find this answer. – Adam Oct 18 '13 at 13:28
  • are you using jQuery or javascript native date – Rituraj ratan Oct 18 '13 at 13:28
  • possible duplicate of [How to get current date in JavaScript](http://stackoverflow.com/questions/1531093/how-to-get-current-date-in-javascript) – Mansfield Oct 18 '13 at 13:28
  • 1
    Whaat have you already tried, show us some code. (Seriously i am going to macro this, how many times i've posted it by now...) – Henk Jansen Oct 18 '13 at 13:28

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,550
  • 2
  • 26
  • 50
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
  • 31,487
  • 5
  • 43
  • 54