1

I have this code:

var Date = "Feb 17, 2012";

How can I convert it to

Date = "17/02/2012"

using jQuery?

Andrei RRR
  • 3,068
  • 16
  • 44
  • 75

4 Answers4

3

Converting with DateJs should be as easy as:

var d1 = Date.parse('2010-10-18, 10:06 AM');
alert(d1.toString('dd/mm/yyyy HH:mm:ss GMT'));
Yoko Zunna
  • 1,804
  • 14
  • 21
  • Have you tried this ? it doesnt work ...http://jsfiddle.net/y459y/ toString() does not have any parameters https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/toString – Manse Feb 17 '12 at 11:11
3

You can use Date() constructor to parse your date :

var mydate = new Date("Feb 17, 2012");

then you have to build up your required format as follows, in this case DD/MM/YYYY :

var newdate = mydate.getDate() + '/' +
              ("0" + (parseInt(mydate.getMonth())+1)).slice(-2) + '/' +
               mydate.getFullYear();

Working example : http://jsfiddle.net/bd3sa/2/

Docs on Date object here

Manse
  • 37,765
  • 10
  • 83
  • 108
0
<input type="text" id="tbDateTime" value="2010-10-18 10:06" />
<input type="text" id="tbDate" value="" />
<input type="text" id="tbTime" value="" />

<input type="button" id="btnSubmit" value="Submit" />


<script type="text/javascript">
    $(function () {
        $('#btnSubmit').click(function () {
            var dateTimeSplit = $('#tbDateTime').val().split(' ');

            var dateSplit = dateTimeSplit[0].split('-');
            var currentDate = dateSplit[2] + '/' + dateSplit[1] + '/' + dateSplit[0];
            //currentDate is 18/10/2010

            $('#tbDate').val(currentDate);

            var currentTime = dateTimeSplit[1];
            //currentTime is 10:06

            $('#tbTime').val(currentTime);
        });
    });
</script>
Yoko Zunna
  • 1,804
  • 14
  • 21
0

If you are using jQuery UI, you can do:

$.datepicker.formatDate( "dd/mm/yy", new Date("Feb 17, 2012") )

Demo: http://jsfiddle.net/wztEA/

welldan97
  • 3,071
  • 2
  • 24
  • 28