Facts:
- In SQL Server there is the column type
datetime
anddate
- Using EF, both
datetime
anddate
are mapped to C#'sDateTime
- In my
datetime
columns I'm always storing the UTC date. - In my current database there are no
date
columns, but I need to use it in a new table I'm going to have - My typical data flow is:
SQL Server --> C# (server side) --> JSON (client side)
I've set my JSON Serializer to always return DateTime
variables to the client with the format yyyy-MM-ddThh:mm:ssZ
, which is a good format for JavaScript. The Z at the end ("zulu") declares that it is in UTC timezone and so my client app knows that it needs to fix the timezone at display. Until now, everything needed to be timezone-fixed at display (because everything was UTC)
Because I want to start using the SQL Server type date
, that Z
at the end will be bothering in those date
cases (but still needed in those datetime
cases). As everything in my serverside is DateTime
, I'm not sure how to make the JSON Serializer know which kind of DateTime
is he looking at...
My question is... is it possible for the JsonSerializerSettings
to know if he's converting a DateTime
that came from a date
or a datetime
? If not, what's a good approach to solve this?
This is my Serializer:
public class JsonNetResult : ActionResult
{
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public object Data { get; set; }
public JsonSerializerSettings SerializerSettings { get; set; }
public Formatting Formatting { get; set; }
public JsonNetResult()
{
var isoDateTimeConverter = new IsoDateTimeConverter();
isoDateTimeConverter.DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
SerializerSettings = new JsonSerializerSettings();
SerializerSettings.Converters.Add(isoDateTimeConverter);
}
public override void ExecuteResult(ControllerContext context)
{
...
}
}
And this is how I use it:
return new JsonNetResult()
{
Data = new
{
persons = dbContext.Persons.Select(e => new
{
name = e.Name,
someDate1 = e.SomeDate1, // datetime
someDate2 = e.SomeDate2 // date
})
}
};