1

I have got the following dates (shown using HTML) from JSON.NET.

<span class="date"> 2013-01-01T00:00:00  </span>
<span class="date"> 2009-05-01T00:00:00  </span>
<span class="date"> 2011-01-06T00:00:00  </span>
<span class="date"> 2012-03-09T00:00:00  </span>

How can I covenrt these dates to the format 'mm-dd-yyyy'. That means, the HTML should be like this:

<span class="date"> 01-01-2013  </span>
<span class="date"> 05-01-2009  </span>
<span class="date"> 01-06-2011  </span>
<span class="date"> 03-09-2012  </span>

I tried several ways using jQuery DatePicker and DateFormat. I also got similar questions on SO. But still I coudn't figure this out. Any help will be greatly appreciated.

2 Answers 2

2

jQuery UI datepicker.parseDate can't parse times, so you'll have to remove them:

var input = "2013-01-01T00:00:00";
input = input.split("T")[0]; // "2013-01-01"
var date = $.datepicker.parseDate("yy-mm-dd", input); 

Then using formatDate shouldn't be a problem:

var newDateString = $.datepicker.formatDate("mm-dd-yy", date);

To get the span html:

var spanHtml = "<span class='date'> " + newDateString + " </span>";
Sign up to request clarification or add additional context in comments.

1 Comment

How can I get this output <span class="date"> 01-01-2013 </span> after using formatDate?
1
$('span').text(function(i, v){
   var d = new Date($.trim(v));
   return (d.getMonth()+1) + '-' + d.getDate() + '-' + d.getFullYear();
});

http://jsfiddle.net/BdMfp/

2 Comments

It cannot convert correctly. E.g. the date'2014-01-01T00:00:00' converts to 12-31-2013.
I tested it using my ASP.NET website and using your link. Both websites show earlier dates. I am running Chrome on XP.

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.