Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The 3 elements needed to have a nice JavaScriptSerializer DateTime experience in older browsers and using ASP.NET WebForms
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace MyNamespace
{
/// <summary>
/// A custom DateTime JavaScriptConverter courtesy of these good folks: http://blog.calyptus.eu/seb/2011/12/custom-datetime-json-serialization/
/// Using this forces DataTimes to be serialised as ISO 8601 rather "\/Date(1249335477787)\/" style
/// </summary>
public class DateTimeJavaScriptConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
return new JavaScriptSerializer().ConvertToType(dictionary, type);
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
if (!(obj is DateTime)) return null;
return new CustomString(((DateTime)obj).ToUniversalTime().ToString("O"));
}
public override IEnumerable<Type> SupportedTypes
{
get { return new[] { typeof(DateTime) }; }
}
private class CustomString : Uri, IDictionary<string, object>
{
public CustomString(string str)
: base(str, UriKind.Relative)
{
}
void IDictionary<string, object>.Add(string key, object value) { throw new NotImplementedException(); }
bool IDictionary<string, object>.ContainsKey(string key) { throw new NotImplementedException(); }
ICollection<string> IDictionary<string, object>.Keys { get { throw new NotImplementedException(); } }
bool IDictionary<string, object>.Remove(string key) { throw new NotImplementedException(); }
bool IDictionary<string, object>.TryGetValue(string key, out object value) { throw new NotImplementedException(); }
ICollection<object> IDictionary<string, object>.Values { get { throw new NotImplementedException(); } }
object IDictionary<string, object>.this[string key]
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item) { throw new NotImplementedException(); }
void ICollection<KeyValuePair<string, object>>.Clear() { throw new NotImplementedException(); }
bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item) { throw new NotImplementedException(); }
void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) { throw new NotImplementedException(); }
int ICollection<KeyValuePair<string, object>>.Count { get { throw new NotImplementedException(); } }
bool ICollection<KeyValuePair<string, object>>.IsReadOnly { get { throw new NotImplementedException(); } }
bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item) { throw new NotImplementedException(); }
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator() { throw new NotImplementedException(); }
IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }
}
}
}
//===============================================================================
// Parse ISO 8601 Date Format date string and return a date - equivalent to https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse
// Found here: n8v.enteuxis.org/2010/12/parsing-iso-8601-dates-in-javascript/
//===============================================================================
function parseISO8601Date(s) {
// parenthese matches:
// year month day hours minutes seconds
// dotmilliseconds
// tzstring plusminus hours minutes
var re = /(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)(\.\d+)?(Z|([+-])(\d\d):(\d\d))/;
var d = [];
d = s.match(re);
// "2010-12-07T11:00:00.000-09:00" parses to:
// ["2010-12-07T11:00:00.000-09:00", "2010", "12", "07", "11",
// "00", "00", ".000", "-09:00", "-", "09", "00"]
// "2010-12-07T11:00:00.000Z" parses to:
// ["2010-12-07T11:00:00.000Z", "2010", "12", "07", "11",
// "00", "00", ".000", "Z", undefined, undefined, undefined]
if (!d) {
throw "Couldn't parse ISO 8601 date string '" + s + "'";
}
// parse strings, leading zeros into proper ints
var a = [1, 2, 3, 4, 5, 6, 10, 11];
for (var i in a) {
d[a[i]] = parseInt(d[a[i]], 10);
}
d[7] = parseFloat(d[7]);
// Date.UTC(year, month[, date[, hrs[, min[, sec[, ms]]]]])
// note that month is 0-11, not 1-12
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/UTC
var ms = Date.UTC(d[1], d[2] - 1, d[3], d[4], d[5], d[6]);
// if there are milliseconds, add them
if (d[7] > 0) {
ms += Math.round(d[7] * 1000);
}
// if there's a timezone, calculate it
if (d[8] != "Z" && d[10]) {
var offset = d[10] * 60 * 60 * 1000;
if (d[11]) {
offset += d[11] * 60 * 1000;
}
if (d[9] == "-") {
ms -= offset;
}
else {
ms += offset;
}
}
return new Date(ms);
}
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<!--
This line of config means that when a JavaScriptSerializer is used by a web service / page method
it will automatically register the DateTimeJavaScriptConverter to use. To use the converter directly in code you would need to enter the below:
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.RegisterConverters(new JavaScriptConverter[] { new DateTimeJavaScriptConverter() });
-->
<jsonSerialization>
<converters>
<add name="DateTimeJavaScriptConverter" type="MyNamespace.DateTimeJavaScriptConverter"/>
</converters>
</jsonSerialization>
</webServices>
<scriptResourceHandler enableCompression="false" enableCaching="true"/>
</scripting>
</system.web.extensions>
</configuration>
@christiaanwesterbeek

This comment has been minimized.

Copy link

christiaanwesterbeek commented Sep 27, 2012

I believe parseISO8601Date is wrong. I posted a comment on the authors site.

The + and – to calculate the time based on the timezone goes in the wrong direction!!!!

When the time being described is: 2012-09-27T09:00:00.000+02:00

It means that the 9am is at a place where the time is 2 hours later then in GMT/UTC. So whenever you see +, the UTC time is minus the time being described. So the last part of your code should actually be:

if (d[9] == “+”) { ms -= offset; } else { ms += offset; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.