jQuery Get Todays Date dd/mm/yyyy

Sam Deering
Tweet

JavaScript code snippet to get the current date in the format dd/mm/yyyy. The month is formatted to prefix with a zero (ie “04”) for single month figures. Also, here is how you can get a future date using jQuery

var fullDate = new Date()
console.log(fullDate);
//Thu May 19 2011 17:25:38 GMT+1000 {}
 
//convert month to 2 digits
var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);
 
var currentDate = fullDate.getDate() + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
console.log(currentDate);
//19/05/2011

Note: the console.log() commands are just for use with firebug.

If the above code doesn’t work try this (thanks pnilesh):

var fullDate = new Date();console.log(fullDate);
var twoDigitMonth = fullDate.getMonth()+"";if(twoDigitMonth.length==1)	twoDigitMonth="0" +twoDigitMonth;
var twoDigitDate = fullDate.getDate()+"";if(twoDigitDate.length==1)	twoDigitDate="0" +twoDigitDate;
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();console.log(currentDate);

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • adrian

    Hi, well done, just a little bug here:
    //Thu May 19 2011 17:25:38 GMT+1000 {}
    //19/04/2011

    May is not the 4th Month, but the 5th

    • Edilberto De león Ortega

      hello, change this instruction:
      var twoDigitMonth = fullDate.getMonth()+””;
      var twoDigitMonth = (fullDate.getMonth()+1)+””;

  • jquery4u

    Thanks Adrian, I’ve fixed it up! :-)

  • kkb

    You just fixed the text and not the script :)

    The results returned by getMonth() are counted from zero in javascript, 0 is January and 11 is December

    • jquery4u

      Hi kkb,

      My bad I totally missed that! It should be fixed now.

      Thanks
      Sam

  • margot sheehan

    Is this actually jQuery? It looks like just raw js to me. (Not that there’s anything wrong with that.)

    • http://jquery4u.com/ jQuery4u

      Yup, good ole JavaScript!

  • http://myemploi-algerie.com/ Dz emploi

    Thanks;
    but how we can output it. in html. do we use dociment.write?

  • Alex

    that helped a lot thankz

  • http://www.facebook.com/lalit.yadav.37017 Lalit Yadav

    var currentDate = fullDate.getDate() + “/” + (fullDate.getMonth()-1) + “/” + fullDate.getFullYear();
    we have to subtract -1 from month while converting in to date format dd/mm/yyyy
    similarly we have to add +1 while converting dd/mm/yyyy to Thu May 19 2011 17:25:38 GMT+1000 {} fromat

    ex:- var Dob= new date(2012, (1 – 1), 20, 5, 0, 0);
    2012 is year ,
    1 is month (1-1 is 0 but the output would be jan) ,
    20 is date,
    rest is time

    • http://jquery4u.com/ jQuery4u

      Hi Lalit,

      Whats the reason for (1-1) for month?

      Surely this: (1-1) is the same as 0

      http://jsfiddle.net/GbPzs/

    • http://jquery4u.com/ jQuery4u

      Hi Lalit,
      Whats the reason for (1-1) for month?
      Surely this: (1-1) is the same as 0
      http://jsfiddle.net/GbPzs/

      • Lalit Yadav

        hi,
        it is functionality of date in jquery.month start from 0 to 11 in jquery date, 0 is jan and 11 is December

  • pnilesh

    Above code is having issue . Use below code

    var fullDate = new Date();console.log(fullDate);
    var twoDigitMonth = fullDate.getMonth()+””;if(twoDigitMonth.length==1) twoDigitMonth=”0″ +twoDigitMonth;
    var twoDigitDate = fullDate.getDate()+””;if(twoDigitDate.length==1) twoDigitDate=”0″ +twoDigitDate;
    var currentDate = twoDigitDate + “/” + twoDigitMonth + “/” + fullDate.getFullYear();console.log(currentDate);

    • http://jquery4u.com/ jQuery4u

      Thanks pnilesh, both code works for me but i have added you version.
      Sam

  • George Lanes

    try this, very simple.

    function pad2(number) {
    return (number < 10 ? '0' : '') + number
    }

    var fullDate = new Date();
    var twoDigitMonth =pad2(fullDate.getMonth());

    console.log(twoDigitMonth);

  • Bùi Minh Đức

    The code : var twoDigitMonth = fullDate.getMonth()+””; give me mothn – 1 :| Why ?

  • Alex Metola

    Solve this fullDate.getMonth()+1.
    If that is wrong.