2
var dt="29/05/2013"; //DD/MM/YYYY

I want to convert to yyyy/MM/dd format;

The approach I've used is:

var newdate=dt.getFullYear()+"/"+dt.getMonth()+"/"+dt.getDate();

Is there any simple approach to convert it with out using substring?

I don't want to use plugins by the way...

Pearl
  • 8,373
  • 8
  • 40
  • 59

5 Answers5

3
var dt = "29/05/2013";
var newdt = dt.split('/').reverse().join('/');

Fiddle:http://jsfiddle.net/4WGnC/

Prasath K
  • 4,950
  • 7
  • 23
  • 35
3

Consider using a library to help you: http://www.datejs.com/ and http://momentjs.com/ are good libraries.

Date.parse ("29/05/2013").toString("yyyy/MM/dd")
jammykam
  • 16,940
  • 2
  • 36
  • 71
2

simply try like this

var a = dt.split("/");
var new_date = a[2]+'/'+a[1]+'/'+a[0];
alert(new_date);
GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • It prints invalid date... Check your answer here http://jsfiddle.net/Njgx9/ – Prasath K May 29 '13 at 06:51
  • Try my edit,I got some problem in my first ,but prevoius i worked with it – GautamD31 May 29 '13 at 06:55
  • 2
    The edit would work; what surprises me is that you managed to get +3 on a bogus answer before =/ – Ja͢ck May 29 '13 at 06:56
  • I had to double check that original code, I thought I had missed something in JS! – jammykam May 29 '13 at 06:57
  • 1
    @Gautam3164 Make sure whether it works well before you post it an answer – Prasath K May 29 '13 at 06:57
  • I already told that,my previous will work...iam sure but i dont know where the mistake,and look I will trace it – GautamD31 May 29 '13 at 06:57
  • @Jack Same surprise for me too .. The Upvoters didn't even check it – Prasath K May 29 '13 at 06:57
  • 1
    @PrasathK bec the upvoters know that it will work....you can check that solution even in forums.Dont blame the upvoters,they are not your freinds and not of mine... – GautamD31 May 29 '13 at 06:58
  • 3
    That's crap! `Date` constructor only supports iso8601 reliably and `Date.toString()` doesn't use a date format argument; no matter now, but still good to remember. – Ja͢ck May 29 '13 at 06:59
  • @Gautam3164 Yupp it will work but yours doesn't ... Anyway cool buddy ...!!! – Prasath K May 29 '13 at 06:59
  • 2
    The fact that it worked for you before is probably because you were using date.js. – Ja͢ck May 29 '13 at 07:11
  • yah @Jack you are right and we are here to help only,and may be its my mistake bec i didnt used .parse of my date for the previuos ans,after I have seen jammaykam ans I see i forgotted – GautamD31 May 29 '13 at 07:13
1

If you are using jquery ui..

var newdate = $.datepicker.formatDate('yy/mm/dd', new Date());
Mayank Awasthi
  • 337
  • 3
  • 14
1

Look this Example:

    var d = new Date ( "January 6, 1972" );

    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; //Months are zero based
    var curr_year = d.getFullYear();
    document.write(curr_date + "-" + curr_month + "-" + curr_year);

Hope it will help you!

webGautam
  • 555
  • 1
  • 3
  • 14
  • Your link is really helpful.... – Pearl May 29 '13 at 07:18
  • 1
    [10 useful date and time format](http://www.webdevelopersnotes.com/tips/html/10_ways_to_format_time_and_date_using_javascript.php3) and [Working with dates] (http://www.elated.com/articles/working-with-dates/) are both useful link! I am happy that you like it! thank you :) – webGautam May 29 '13 at 07:25