0

I have string like this:

 "/Date(1388521800000)/"

How would I convert it to "YYYY-MM-DD"?

Andrew Koroluk
  • 611
  • 6
  • 19
Mohammad Zargarani
  • 1,422
  • 2
  • 12
  • 19

3 Answers3

2

If the String always looks the same I would parse it like this:

String date = "/Date(1388521800000)/";
String stamp;

stamp = date.substring(date.indexOf("(")+1, date.indexOf(")"));
sagschni
  • 89
  • 1
  • 13
1

you can use this function to parse such date.

function fnParseDate(dateString)
{
    if (typeof dateString == 'object' && dateString instanceof Date) // It's already a JS Object (Of Date Type)
        return dateString; // No need to do any parsing. Return the original value.

    if (dateString.indexOf('/Date(') == 0) //Format: /Date(1320259705710)/
        return new Date(parseInt(dateString.substr(6), 10));

    alert('Given date is no properly formatted: ' + dateString);
    return new Date(); //Default value!
}
Sohil Desai
  • 2,940
  • 5
  • 23
  • 35
  • 1
    Given the OP, `new Date(+('/Date(1388521800000)/'.replace(/\D/g,'')))` is sufficient to create a suitable Date. Or `new Date(+('/Date(1388521800000)/'.match(/\d+/)[0]))`. – RobG Feb 20 '14 at 13:23
1
var sDate = "/Date(1388521800000)/";
var date = new Date(sDate.match(/\/Date\(([0-9]+)\)\//)[1]|0);
var formattedDate = date.getFullYear() + "-"+ 
                   (date.getMonth()<9?"0"+(date.getMonth()+1):date.getMonth()+1)+"-"+
                   (date.getDate()<10?"0"+date.getDate():date.getDate());
guy777
  • 222
  • 1
  • 14