Reputation: 2449
I have a variable which stores the date as "18/07/2013". I need to parse this to "07/18/2013". How this is possible in jquery?
var date = $("#dte").val();
Upvotes: 0
Views: 1142
Reputation: 1714
Try this out. It helped me to format date in jQuery
$.datepicker.formatDate('dd/mm/yy', new Date(yourDateString))
Upvotes: 1
Reputation: 318162
javascript's Date accepts a string in the format year / month / day
, so just reverse the order and create a date object :
var date = '18/07/2013';
var dObj = new Date(date.split('/').reverse().join('/'));
var newD = dObj.getDate() + '/' + (dObj.getMonth()+1) + '/' + dObj.getFullYear();
Upvotes: 0
Reputation: 15356
Try this:
var date = $("#dte").val().split('/');
var newDate = date[1]+'/'+date[0]+'/'+date[2];
alert(newDate); // or $("#dte").val(newDate); if you want to update the input
Upvotes: 1
Reputation:
var date = $("#dte").val();
d=date.split('/');
newdate=d[2]+"/"+d[1]+"/"+d[0];
return newdate;
Upvotes: 2
Reputation: 4271
var date = date.replace(/(\d\d)\/(\d\d)\/(\d\d)/, "$2/$1/$3");
Upvotes: 0
Reputation: 29
There is jQuery dateFormat jquery plugin..you can use that for show date format.
Upvotes: 0
Reputation: 22617
A simple replace would do the trick:
date = date.replace(/^(\d\d)\/(\d\d)\//, "$2/$1/");
No need of jQuery for this.
Upvotes: 1
Reputation: 664164
You could use
var date = $("#dte").val().replace(/^(\d\d)\/(\d\d)/, "$2/$1");
to swap month and date part.
Upvotes: 1