56

We are sending JSON to an API defined by swagger that some properties are DateTime in the format yyyy-MM-ddThh:mm:ss.000Z (the milliseconds must be 3 digits or it fails validation at the endpoint) and some are Date (no time) properties.

I have seen many messages saying use the formatters like this:

var jsonSettings = new JsonSerializerSettings();
jsonSettings.DateFormatString = "yyyy-MM-ddThh:mm:ss.000Z"; //try .fffZ too
var jsonObject= Newtonsoft.Json.JsonConvert.DeserializeObject<OurSwaggerObject>(json , setting);

but this does not convert the DateTimes into the correct format, and how does C# deal with a Date only type? It always seems to serialise as DateTime.MinValue()

Here is an example:

Someone sends me json as string but the the dates and datetimes in the incorrect format to be sent to the endpoint. I was hoping that the swagger class and json deserialisation would format them but it is not.

This is the swagger generated class

 public class OurSwaggerObject
    {
        [Newtonsoft.Json.JsonProperty("dateTimeField", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Required]
        [System.ComponentModel.DataAnnotations.RegularExpression(@"^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$")]
        public DateTime dateTimeField { get; set; }

        [Newtonsoft.Json.JsonProperty("dateField", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Required]
        [System.ComponentModel.DataAnnotations.RegularExpression(@"^\d{4}-\d\d-\d\d$")]
        public DateTime dateField { get; set; }
    }

So I try and coerce the json to be correct but I'm doing it wrong or something is missing

string json = @"{ 'dateTimeField': '1995-04-07T00:00:00',
                          'dateField': '1995-04-07T00:00:00'
                           }";

        /* The json we need to satisfy the swagger endpoint is:

          { 'dateTimeField': '1995-04-07T00:00:00.000Z',
            'dateField': '1995-04-07'
                           }              
          */

        OurSwaggerObject deserialisedIntoObject = Newtonsoft.Json.JsonConvert.DeserializeObject<OurSwaggerObject>(json);

        string serialisedToString = Newtonsoft.Json.JsonConvert.SerializeObject(deserialisedIntoObject);
        //serialisedToString= "{\"dateTimeField\":\"1995-04-07T00:00:00\",\"dateField\":\"1995-04-07T00:00:00\"}"

        var jsonSettings = new JsonSerializerSettings();
        jsonSettings.DateFormatString = "yyyy-MM-ddThh:mm:ss.fffZ"; //this won't help much for the 'date' only field!
        deserialisedIntoObject = Newtonsoft.Json.JsonConvert.DeserializeObject<OurSwaggerObject>(json,jsonSettings);
        serialisedToString = Newtonsoft.Json.JsonConvert.SerializeObject(deserialisedIntoObject, jsonSettings);
        //serialisedToString="{\"dateTimeField\":\"1995-04-07T00:00:00\",\"dateField\":\"1995-04-07T00:00:00\"}"
8
  • Please provide an mcve that illustrates the problem
    – NineBerry
    Commented Sep 8, 2017 at 15:44
  • Json.NET uses the de-facto standard since version 4.5 (current one is 10.x). It does serialize the date correctly. Format strings were only used for older versions. Post code that actually shows the problem Commented Sep 8, 2017 at 15:45
  • On other hand, if the other endpoint requires an arbitrary number of milliseconds, the problem is theirs. ISO8601 doesn't require milliseconds Commented Sep 8, 2017 at 15:47
  • As for date only - Json doesn't have dates or times. That's why mentioned the defacto standard. At some point people simply decided to start using ISO8601. A date can be represented by a DateTime value. You can serialize it to Json either as a full ISO8601 string with zero hour,minute, or as a date-only string. All are valid. Commented Sep 8, 2017 at 15:50
  • Swagger OpenAPi does define date and datetime swagger.io/specification I will post up some code example
    – DomBat
    Commented Sep 8, 2017 at 16:05

5 Answers 5

100

As I mentioned in a comment, there is no standard date representation in JSON. The ISO8601 is the de-facto standard, ie most people started using this some years ago. ISO8601 does not require milliseconds. If the other endpoint requires them, it's violating the defacto standard.

Json.NET has been using IOS8601 since version 4.5. The current one is 10.0.3. The following code:

JsonConvert.SerializeObject(DateTime.Now)

returns

"2017-09-08T19:01:55.714942+03:00"

On my machine. Notice the timezone offset. That's also part of the standard. Z means UTC.

You can specify your own time format, provided it's the correct one. In this case, it should be yyyy-MM-ddTHH:mm:ss.fffZ. Notice the fff for milliseconds and HH for 24-hour.

The following code

var settings = new JsonSerializerSettings {
    DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffZ"
};
var json = JsonConvert.SerializeObject(DateTime.Now, settings);

returns

"2017-09-08T19:04:14.480Z"

The format string does not force a timezone translation. You can tell Json.NET to treat the time as Local or Utc through the DateTimeZoneHandling setting:

var settings = new JsonSerializerSettings {
    DateFormatString = "yyyy-MM-ddTH:mm:ss.fffZ",
    DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
var json = JsonConvert.SerializeObject(DateTime.Now, settings);

Returns :

"2017-09-08T16:08:19.290Z"

UPDATE

As Matt Johnson explains, Z is just a literal, while K generates either Z or an offset, depending on the DateTimeZoneHandling setting.

The format string yyyy-MM-ddTH:mm:ss.fffK with DateTimeZoneHandling.Utc:

var settings = new JsonSerializerSettings {
    DateFormatString ="yyyy-MM-ddTH:mm:ss.fffK",
    DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
var json = JsonConvert.SerializeObject(DateTime.Now, settings);

Will return :

2017-09-11T9:10:08.293Z

Changing to DateTimeZoneHandling.Utc will return

2017-09-11T12:15:12.862+03:00

Which, by the way is the default behaviour of Json.NET, apart from the forced millisecond precision.

Finally, .NET doesn't have a Date-only type yet. DateTime is used for both dates and date+time values. You can get the date part of a DateTime with the DateTime.Date property. You can retrieve the current date with DateTime.Today.

Time of day is represented by the TimeSpan type. You can extract the time of day from a DateTime value with DateTime.TimeOfDay. Timespan isn't strictly a time-of-day type as it can represent more than 24 hours.

What was that yet?

Support for explicit Date, TimeOfDay is comming through the CoreFX Lab project. This contains "experimental" features that are extremely likely to appear in the .NET Runtime like UTF8 support, Date, String, Channles. Some of these already appear as separate NuGet packages.

One can use the System.Time classes already, either by copying the code or adding them through the experimental NuGet source

3
  • 2
    Z in a format string is a literal. You should use K, which properly reflects the DateTimeKind associated with the DateTime in question. Commented Sep 8, 2017 at 21:28
  • True - I never had to hand-code the ISO8601 format Commented Sep 11, 2017 at 9:02
  • 1
    Interestingly enough when I use this var settings=new JsonSerializerSettings{DateFormatString ="yyyy-MM-ddTHH:mm:ss.fffZ"}; var json=JsonConvert.SerializeObject(DateTime.Now,settings); it produces this "effective": { "start": "\"2021-04-09T09:52:47.170Z\"", "end": "\"2023-04-08T09:52:47.170Z\"" }, NOTE the additional speech marks and \
    – Gwasshoppa
    Commented Apr 9, 2021 at 0:10
3

Get current universaltime to json date time format and vice versa:

DateTime currentDateTime = DateTime.Now.ToUniversalTime();
var jsonDateTime = GetJSONFromUserDateTime(currentDateTime);
DateTime getDateTime = GetUserDateTimeFromJSON(jsonDateTime);

Here are both methods:

/// <summary>
/// Convert UserDateTime({9/7/2018 8:37:20 AM}) to JSON datetime(1536309440373) format
/// </summary>
/// <param name="givenDateTime"></param>
/// <returns></returns>
public static string GetJSONFromUserDateTime(DateTime givenDateTime)
{
    string jsonDateTime = string.Empty;
    if (givenDateTime != null)
    {
        JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
        {
            DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
        };
        jsonDateTime = JsonConvert.SerializeObject(givenDateTime, microsoftDateFormatSettings);
        jsonDateTime = jsonDateTime.Replace("\"\\/Date(", "").Replace(")\\/\"", "");
    }
    return jsonDateTime;
}

/// <summary>
/// Convert JSON datetime(1536309440373) to user datetime({9/7/2018 8:37:20 AM})
/// </summary>
/// <param name="jsonDateTime"></param>
/// <returns></returns>
public static dynamic GetUserDateTimeFromJSON(string jsonDateTime)
{
    dynamic userDateTime = null;
    if (!string.IsNullOrEmpty(jsonDateTime))
    {
        JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
        {
            DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
        };
        userDateTime = JsonConvert.DeserializeObject("\"\\/Date(" + jsonDateTime + ")\\/\"", microsoftDateFormatSettings);
    }
    return userDateTime;
}
1
  • About Deserialize from JSON I can add that somethimes you get a shorter number like from the OpenweatherMap API and the solution in this case is to multiplicate the number to 1000 and use your function GetUserDateTimeFromJSON. Commented May 2, 2020 at 18:35
1

If, like Willie Esteche you wish to convert a number that is too low, you have probably got a Unix Date Time value. His solution does indeed work, but as of .Net Framework 4.6 there is an easier way to do things.

Given a value of 1500013000, first you convert this to a DateTimeOffset with the FromUnixTimeSeconds() method, then simply grab the DateTime component.

DateTime dt = DateTimeOffset.FromUnixTimeSeconds(1500013000).UtcDateTime;

Conversion back (assuming UTC) is performed like so:

long Udt = new DateTimeOffset(dt,TimeSpan.Zero).ToUnixTimeSeconds();
1

You can try this:

DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
2
  • It's simple and humble. Thanks ! Commented Mar 20 at 4:32
  • No problem :) Glad that I can help :)
    – YusufKaa
    Commented Jul 12 at 7:25
0

In my case, i using this config in vb.net, it's so easy translate to c #

settings.NullValueHandling = NullValueHandling.Ignore
settings.DateTimeZoneHandling = DateTimeZoneHandling.Local
BE = JsonConvert.DeserializeObject(Of EN_BE)(str, settings)

I get this format date from laravel API

2020-08-02T01:41:05.000000Z 

and i convert to correct value same to database

2020-08-01 20:41:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.