Andrei RRR
Andrei RRR

Reputation: 3162

Convert Feb 17, 2012 to 17/02/2012 with jQuery

I have this code:

var Date = "Feb 17, 2012";

How can I convert it to

Date = "17/02/2012"

using jQuery?

Upvotes: 1

Views: 290

Answers (4)

Manse
Manse

Reputation: 38147

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

Upvotes: 3

welldan97
welldan97

Reputation: 3076

If you are using jQuery UI, you can do:

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

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

Upvotes: 0

Yoko Zunna
Yoko Zunna

Reputation: 1824

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'));

Upvotes: 3

Yoko Zunna
Yoko Zunna

Reputation: 1824

<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>

Upvotes: 0

Related Questions