Format date in jquery- from Sat Mar 03 2012 14:16:05 GMT+0530 to 03/03/2012

12,747

Solution 1

function dateFormat(){
    var d = new Date();

    date = d.getDate();
    date = date < 10 ? "0"+date : date;

    mon = d.getMonth()+1;
    mon = mon < 10 ? "0"+mon : mon;

    year = d.getFullYear()

    return (date+"/"+mon+"/"+year);
}

dateFormat();

Solution 2

All you need to do is tell the date function to format the date to the kind of output you want.

(Assuming you are using date function of JavaScript, which you did mention above.)

http://www.webdevelopersnotes.com/tips/html/10_ways_to_format_time_and_date_using_javascript.php3

<script type="text/javascript">
    <!--

    var d = new Date();  
    var curr_date = d.getDate();  
    var curr_month = d.getMonth(); 
    var curr_year = d.getFullYear();

    document.write(curr_date + "-" + curr_month + "-" + curr_year);

    //-->
</script>

OR

http://blog.stevenlevithan.com/archives/date-time-format

OR

http://archive.plugins.jquery.com/project/jquery-dateFormat

<script>
    $.format.date("2009-12-18 10:54:50.546", "dd/MM/yyyy");
</script>
Share:
12,747
Infinity
Author by

Infinity

Updated on November 21, 2022

Comments

  • Infinity
    Infinity over 1 year

    How can I format this date?

     <li>Sat Mar 03 2012 14:16:05 GMT+0530 (India Standard Time)</li>
    

    I used new date() function to get date in this li. I want date like this 03/03/2012

  • Infinity
    Infinity about 12 years
    :- its giving the output as 3/3/2012 . How can I make it 03/03/2012 ie how do I get zeros before single digits?
  • Infinity
    Infinity about 12 years
    I used ranganadh's answer. You too were a great help. Thanks :)