keep C# datetime local time between json and Web api?

前端 未结 3 1849
死守一世寂寞
死守一世寂寞 2021-02-05 12:55

I have problem when I have datatime in json object it will convert it to UTC time zone in C# dateTime just want to ask how to keep local time?can I set time zone property in w

3条回答
  •  遇见更好的自我
    2021-02-05 13:08

    You can change your serializer settings to use the JSON.net serializer :

    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = 
        new JsonSerializerSettings
        {
            DateFormatHandling = DateFormatHandling.IsoDateFormat,
            DateTimeZoneHandling = DateTimeZoneHandling.Unspecified,
        };
    

    There is also various date format you can choose from : DateTimeZoneHandling

    /// 
    /// Specifies how to treat the time value when converting between string and .
    /// 
    public enum DateTimeZoneHandling
    {
        /// 
        /// Treat as local time. If the  object represents a Coordinated Universal Time (UTC), it is converted to the local time.
        /// 
        Local = 0,
    
        /// 
        /// Treat as a UTC. If the  object represents a local time, it is converted to a UTC.
        /// 
        Utc = 1,
    
        /// 
        /// Treat as a local time if a  is being converted to a string.
        /// If a string is being converted to , convert to a local time if a time zone is specified.
        /// 
        Unspecified = 2,
    
        /// 
        /// Time zone information should be preserved when converting.
        /// 
        RoundtripKind = 3
    }
    

提交回复
热议问题