0

I want to convert date from Format 'dd/MM/yyyy' to 'MM/dd/yyyy' and vice-versa

I tried following method for conversion

var val1 = '1/2/2014';
var newDate1 = new Date(val);

alert(newDate1); //returns Thu Jan 02 2014

var val2 = '2/1/2014';
var newDate2 = new Date(val);

alert(newDate2); // returns Sat Feb 01 2014

It is always considering above values in 'MM/dd/yy' format, in one case i need to take date format as 'dd/MM/yy' and convert it to some other

Is it possible to define the 'Date format' of the 'Date' we are going to convert?

Please suggest..

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Anil D
  • 1,989
  • 6
  • 29
  • 60

3 Answers3

0

You can use dateFormat function written by Steven Levithan.

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • 2
    post some relevant code also as link may get lost in future. – Bhushan Kawadkar Jun 23 '14 at 06:53
  • I don't think it's needed. That link contains link to the downloadable library, which is on the same domain, so if the link gets lost in future, the library will also get lost. I don't think I should post the whole library here :) And IMO this is not one of those temporary domain. – Chankey Pathak Jun 23 '14 at 07:00
  • You should not post whole library but some relevant code that tell how to use library. Probably some sample code. – Bhushan Kawadkar Jun 23 '14 at 07:03
0

Try this:

var days = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];
var date = new Date('02-01-2014');
var day_of_week = date.getDay();
var formattedDate = date.toString(day_of_week+' M dd yyyy');

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

jQuery UI provides a way to set date format, such as:

$.datepicker.formatDate( "yy-mm-dd", new Date( 2007, 1 - 1, 26 ) );

Reference:

- http://api.jqueryui.com/datepicker/
PeterKA
  • 24,158
  • 5
  • 26
  • 48