Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Anyone knows how to convert normal dateformat for ex:2-2-2014 to JSON format.Iam getting this exception DateTime content '2-2-2014' does not start with '\/Date(' and end with ')\/' as required for JSON while serializing.Please reply if u know the answer.


Regards
Vijay
Posted
Comments
Jameel VM 28-Nov-14 6:52am    
can you please post the code?
vijaykittur 28-Nov-14 8:11am    
Hi Jameel please check this code
:Datetime dt='01-01-2014'var javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string datetimeconvert = javaScriptSerializer.Serialize(dt);iam getting runtime exception as stated above in my post.
Jameel VM 28-Nov-14 8:34am    
Can you please change your date string from '01-01-2014' to '01/01/2014' and then try to serialize else convert to datetime DateTime dt =Convert.ToDateTime("01-01-2014"); before serializing it.
vijaykittur 2-Dec-14 2:20am    
thanks it worked..

1 solution

Hi You can user any of these function..
C#
JSON.stringifyWcf = function(json) {

        return JSON.stringify(json, function(key, value) {
            if (typeof value == "string") {
                var a = reISO.exec(value);
                if (a) {
                    var val = '/Date(' + new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])).getTime() + ')/';
                    this[key] = val;
                    return val;
                }
            }
            return value;
        })
    };
    JSON.dateStringToDate = function(dtString) {
        var a = reISO.exec(dtString);
        if (a)
            return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
        a = reMsAjax.exec(dtString);
        if (a) {
            var b = a[1].split(/[-,.]/);
            return new Date(+b[0]);
        }
        return null;
    };
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900