Sandy
Sandy

Reputation: 2449

How to change the date format in jquery?

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

Answers (8)

Karthik AMR
Karthik AMR

Reputation: 1714

Try this out. It helped me to format date in jQuery

 $.datepicker.formatDate('dd/mm/yy', new Date(yourDateString))

Upvotes: 1

adeneo
adeneo

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

FIDDLE

Upvotes: 0

Yotam Omer
Yotam Omer

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

user2188149
user2188149

Reputation:

var date = $("#dte").val();

d=date.split('/');

newdate=d[2]+"/"+d[1]+"/"+d[0];

return newdate;

Upvotes: 2

Jay Harris
Jay Harris

Reputation: 4271

 var date = date.replace(/(\d\d)\/(\d\d)\/(\d\d)/, "$2/$1/$3");

JSFIDDLE

Upvotes: 0

ujjwal
ujjwal

Reputation: 29

There is jQuery dateFormat jquery plugin..you can use that for show date format.

Upvotes: 0

MaxArt
MaxArt

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

Bergi
Bergi

Reputation: 664164

You could use

var date = $("#dte").val().replace(/^(\d\d)\/(\d\d)/, "$2/$1");

to swap month and date part.

Upvotes: 1

Related Questions