Skip to content Skip to sidebar Skip to footer

How To Convert Javascriptserializer Serialized Datetime String To Javascript Date Object

After serializing an object with DateTime field with JavaScriptSerializer, I see that the DateTime field looks like this: EffectiveFrom: '/Date(1355496152000)/' How can I convert

Solution 1:

UPDATE: This answer may not be appropriate in all cases. See JD's answer for an elegant solution that is likely better.

You could just "fix" the output from JavaScriptSerializer on the .Net side of things:

JavaScriptSerializer serializer = new JavaScriptSerializer();
var json = serializer.Serialize(this);
json = Regex.Replace(json,@"\""\\/Date\((-?\d+)\)\\/\""","new Date($1)");
return json;

This would change

EffectiveFrom:"/Date(1355496152000)/"

to

EffectiveFrom:newDate(1355496152000)

Which is directly consumable by Javascript

EDIT: update to accommodate negative dates

EDIT: Here is the Regex line for VB folks:

json = Regex.Replace(json, """\\/Date\((-?\d+)\)\\/""", "new Date($1)")

UPDATE 2016.11.20: With a lot more datetime handling in javascript/json behind me, I would suggest changing the regex to something as simple as

json = Regex.Replace(json,@"\""\\/Date\((-?\d+)\)\\/\""","$1");

The resulting value is valid JSON, and can be converted to a Date object on the javascript side.

It is also worth noting that moment.js (http://momentjs.com/docs/#/parsing/) handles this format happily enough.

moment("/Date(1198908717056-0700)/");

Solution 2:

There is an answer that may help you:

Parsing Date-and-Times from JavaScript to C#

If you want to parse datetime string to datetime value with javascript you must use "new Date" like this:

var data = newDate("1355496152000");

Solution 3:

var obj = { EffectiveFrom: "/Date(1355496152000)/" };

//parse the Date value and replace the property value with Date object:var dateValue = parseInt(obj.EffectiveFrom.replace(/\/Date\((\d+)\)\//g, "$1"));
obj.EffectiveFrom = newDate(dateValue);

Solution 4:

This is a bit of a hack, but the above seemed inelegant for what I'm trying to achieve, so in the object definition I'm serializing, I did this:

///<summary>Date of the record retention event or document date.///</summary>publicstring DateOfRetentionEvent;
        [ScriptIgnore]
        public DateTime RetentionEventDate 
        {
            get
            {
                return _retentionEventDate;
            }
            set
            {
                _retentionEventDate = value;
                DateOfRetentionEvent = value.ToShortDateString();
            }
        }

The point being that, at least in my use case (deserialization never happens), the JSON doesn't really care what C# is doing with the date value. Adding [ScriptIgnore] to the DateTime value and giving an alternative view for the parser to output should do the trick. It does in my case:

{"DateToDispose":"1/1/2020","DateOfRetentionEvent":"10/1/2014","FullRetentionCode":"NR+5","RetentionEvent":"NR","RetentionPeriod":5}

Post a Comment for "How To Convert Javascriptserializer Serialized Datetime String To Javascript Date Object"