Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Current »

Have had some issues with dates returned from REST services.  

Initially I found that the default .net serializer will output the format "Date(n)\"  where n is the number of seconds since 1980 or some such date. 

Not sure how everyone else feels about this format but I think its ugly as hell and difficult to debug and work with.

So next I started returned the dates as strings from the WCF REST service.  

Best practice seems to be to always deal with UTC in ISO8061.  Dates are sent out in UTC format and then formatted to local time on the client.

Here is a couple of small helper methods to deal with it.

Date format including time zone

ToISOLocalDate
 public static string ToISOLocalDate(DateTime dateTime)
        {
            TimeSpan offset = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
            return dateTime.ToString(string.Format("yyyy-MM-ddTHH:mm:ss.00+{0}:{1}", offset.Hours, offset.Minutes == 0 ? "00" : offset.Minutes.ToString()));
        }

I'm using this class in the services, to return a DateTime which includes the timezone offset.

Small disclaimer with the above helper, I am assuming the offset is + from UTC. This may not be the case depending on your location.

When I get some time I'll improve this code.

 

Date format in UTC

ToIsoDate
public static string ToISODate(DateTime dateTime)
        {
            return dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.00Z");
        }

This helper method returns Zulu time.

  • No labels