Sjm Kaushik Sjm Kaushik - 2 months ago 5x
jQuery Question

how to display this string '20150211' as '2015-02-11' using jquery

I am getting date as 20150211, but I want to display it like '2015-02-11' using jquery or any other way.

I tried below code in .cshtml :

foreach (var item in ViewBag.Stats)
<span class="visitDate">@item.ItemArray[0]</span>

<script type="text/javascript">
$(document).ready(function () {
$(".visitDate").text().replace(/\s+/g, '-').toLowerCase();


This should work (assuming you are setting the visit date text to the new format):

var parts = [ text.slice(0, 4), text.slice(4, 6), text.slice(6, 8) ];

Or in C#:

<span class="visitDate">
    var date = DateTime.ParseExact(item.ItemArray[0], "yyyyMMdd", null);