C# 使用System.Text.Json在ASP.NET Core 3.0中格式化DateTime
我正在将web API从.NET Core 2.2迁移到3.0,并希望使用新的C# 使用System.Text.Json在ASP.NET Core 3.0中格式化DateTime,c#,json,datetime,asp.net-core,system.text.json,C#,Json,Datetime,Asp.net Core,System.text.json,我正在将web API从.NET Core 2.2迁移到3.0,并希望使用新的System.Text.Json。使用Newtonsoft时,我能够使用下面的代码格式化DateTime。我怎样才能做到这一点 .AddJsonOptions(options => { options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; opt
System.Text.Json
。使用Newtonsoft
时,我能够使用下面的代码格式化DateTime
。我怎样才能做到这一点
.AddJsonOptions(options =>
{
options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
options.SerializerSettings.DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ";
});
使用自定义格式化程序解决。谢谢Panagiotis的建议
public class DateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Debug.Assert(typeToConvert == typeof(DateTime));
return DateTime.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"));
}
}
// in the ConfigureServices()
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
});
公共类DateTimeConverter:JsonConverter
{
公共覆盖日期时间读取(参考Utf8JsonReader reader,键入typeToConvert,JsonSerializerOptions选项)
{
Assert(typeToConvert==typeof(DateTime));
return DateTime.Parse(reader.GetString());
}
公共重写无效写入(Utf8JsonWriter编写器、日期时间值、JsonSerializerOptions选项)
{
writer.WriteStringValue(value.ToUniversalTime().ToString(“yyyy'-'MM'-'dd'T'HH':'MM':'ssZ”);
}
}
//在ConfigureServices()中
services.AddControllers()
.AddJsonOptions(选项=>
{
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
});
迁移到Core 3时,我必须替换System.Text.Json以再次使用Newtonsoft,方法是:
services.AddControllers().AddNewtonsoftJson();
但我在Angular应用程序中遇到了与UTC日期相同的问题,我必须添加此项以获取UTC中的日期:
services.AddControllers().AddNewtonsoftJson(
options => options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc);
在您的情况下,您应该能够做到这一点:
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
options.SerializerSettings.DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ";
});
这是有效的,我希望它能帮助 真正的问题是什么?如何将日期时间视为UTC(即使不是)?默认情况下,JSON.NET和System.Text.JSON都使用ISO8601。如果DateTimeKind是UTC,则将
Z
追加到字符串。本地时间将包括本地时区偏移,但这不是您的代码所做的,因为JSON.NET已经使用ISO8601—与您使用的格式相同。你在那里所做的是强迫它对所有人使用UTC。我已经解释过System.Text.Json已经处理UTC日期。这意味着您要存储的日期是本地的或未指定的。但为什么要转换为UTC?为什么不让System.Text.Json发出偏移量?在任何情况下,日期格式都在中进行了说明。在创建自定义格式化程序之前,无法强制使用格式。您可以确保使用的所有日期都是UTC,或者使用DateTimeOffset来确保指定了偏移量。我希望序列化DateTime而不使用小数秒,并且始终使用UTC。当使用swift(iOS应用程序)访问我的API时,小数秒和偏移量会导致json解析失败。此处的相关问题:此代码有两个问题。1) NewtonSoft.Json并不总是对值调用.ToUniversalTime()
。它取决于它的DateTimeKind
。提供的格式字符串去除了NewtonSoft.Json
mantains的7位小数精度。如果你同意,我可以用正确的代码更新你的答案。否则我可以用正确的代码创建一个新答案。问题是关于System.Text.Json而不是NewtonSoft.Json因为我已经在客户端代码中使用了UTC,所以我排除了ToUniversalTime()
,我所需要的只是日期字符串末尾的Z
,这就满足了,我想这是正确的方法?!。显然,reader
有一个方法reader.GetDateTime()
,可以在Read
method中使用。这个问题不是关于newtonsoft.json的