admin管理员组

文章数量:1414881

I am getting a date from my api and the format i get is "2019-04-17T15:04:28" the date is UTC. Should the date i get back have the Z at the end or doesn't it matter. Javascript date function will display incorrect if the Z is not there wont it?

Thanks @Esko thanks i think for me the confusion is that in core if you change the json serializer options in startup.cs by the following:

AddJsonOptions(opt =>{ 
  opt.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
            });

the tool tip in visual studio says yet it doesn't put the Z on and the documentation also doesn't show the Z (.htm)

Instead i am going to try and set a different option

opt.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;

I am getting a date from my api and the format i get is "2019-04-17T15:04:28" the date is UTC. Should the date i get back have the Z at the end or doesn't it matter. Javascript date function will display incorrect if the Z is not there wont it?

Thanks @Esko thanks i think for me the confusion is that in core if you change the json serializer options in startup.cs by the following:

AddJsonOptions(opt =>{ 
  opt.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
            });

the tool tip in visual studio says yet it doesn't put the Z on and the documentation also doesn't show the Z (https://www.newtonsoft./json/help/html/T_Newtonsoft_Json_DateFormatHandling.htm)

Instead i am going to try and set a different option

opt.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
Share Improve this question edited Apr 10, 2019 at 15:33 JimmyShoe asked Apr 10, 2019 at 11:50 JimmyShoeJimmyShoe 2,2995 gold badges25 silver badges45 bronze badges 6
  • You need to explain what you want to do with this date. – Quentin S. Commented Apr 10, 2019 at 11:52
  • If it's your api, why doesn't it return the Z if the date is infact UTC? If the api is not done by you, and you are certain the times are UTC, just append the Z after fetching if needed. Don't now exactly what is your question beyond that. – Esko Commented Apr 10, 2019 at 11:53
  • i will be using a third party grid to display rows of data. It takes a date object and displays the date and time in local time – JimmyShoe Commented Apr 10, 2019 at 11:53
  • the api is from another team member and they are using json serializer - it doesn't add the z – JimmyShoe Commented Apr 10, 2019 at 11:56
  • @JimmyShoe You can control how json serializes dates. newtonsoft./json/help/html/… – Esko Commented Apr 10, 2019 at 11:59
 |  Show 1 more ment

2 Answers 2

Reset to default 3

I had the similar problem and I found the answer
In my case the DateTime was with Kind Unspecified, in this case the JsonConvert hasn't enough information to add the Z in the end.

You have several options:

  1. Convert it to UTC with ToUniversalTime method
  2. If you're sure you're working with UTC you can add it to general JsonOptions like this:
JsonConvert.DefaultSettings = (() =>
{
    return new JsonSerializerSettings
    {
        Converters = new List<JsonConverter>() 
        {
            new IsoDateTimeConverter 
            { 
                DateTimeStyles = System.Globalization.DateTimeStyles.AssumeUniversal 
            }
        }
    };
});
  1. if you just want to add the Kind=UTC to current Unspecified Date, you can always write isoConverter like this:
JsonSerializerSettings
{
    Converters = new List<JsonConverter>() {  new CustomIsoDateTimeConverter() }
};

Where the class will look like this:

public class CustomIsoDateTimeConverter : IsoDateTimeConverter
{
    public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
    {
        if (value is DateTime dateTime && dateTime.Kind == DateTimeKind.Unspecified)
            value = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);

        base.WriteJson(writer, value, serializer);
    }
}

If you know the timezone to be constant, you can always just append +0Z00 to your datestring. For example, if the server serializes time into CEST, turn "2019-04-17T15:04:28" into "2019-04-17T15:04:28+0100"

Parsing "2019-04-17T15:04:28" in new Date will parse it as if it's in your local timezone - the meaning of this string depends on what timezone it's parsed in.

Parsing "2019-04-17T15:04:28Z" will parse it in GMT - meaning that no matter what system parses it, it will always refer to the same time.

In other words, "2019-04-17T15:04:28" as a timestamp is ambiguous unless you know the timezone it was recorded in.

本文标签: c datetime utc string and the z when using in javascriptStack Overflow