0

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();
3
  • It's impossible in jQuery. jQuery does not have any date formatting tools. Commented Jul 17, 2013 at 15:02
  • This is what regular JavaScript is capable of! Commented Jul 17, 2013 at 15:03
  • If you need to do anything more complicated with those dates, consider using Moment.js. Commented Jul 17, 2013 at 15:08

8 Answers 8

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

d=date.split('/');

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

return newdate;
Sign up to request clarification or add additional context in comments.

Comments

1

You could use

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

to swap month and date part.

Comments

1

A simple replace would do the trick:

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

No need of jQuery for this.

Comments

1

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

Comments

1

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

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

Comments

0

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

Comments

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

JSFIDDLE

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.