【问题标题】:The Date becomes one day behind after passing from Angular 9 to Web API of ASP.NET Core从 Angular 9 传递到 ASP.NET Core 的 Web API 后,日期变为落后一天
【发布时间】:2020-07-30 07:45:57
【问题描述】:

从 Angular 9 传递到 ASP.NET Core 的 Web API 后,日期会晚一天。从角度来看,日期正确传递 从角度将其传递为“2020 年 7 月 30 日星期四 00:00:00 GMT+0530(印度标准时间)”,但是当它调用 api 时,它变为“29-07-2020 18:30:00”

【问题讨论】:

    标签: angular asp.net-core asp.net-web-api


    【解决方案1】:

    这些日期是相等的,第一个是包含时区偏移,第二个是 UTC。 您必须将日期构造为 UTC 日期,然后您将摆脱时区偏移。

    var dt=new Date(Date.UTC(yyyy, mm, dd))

    yyyy 是年,mm 是从零开始的月份,dd 是天...

    【讨论】:

    • 换句话说:它们描述了相同的instant,只是在不同的时区。
    【解决方案2】:
    Web API was converting it so I founded a solution from internet after lot of research
     modify startup.cs file as
    services.AddControllers().AddJsonOptions(options =>
    {
    options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
     });
    
    create a new class and paste the following lines
    
    using System.Text.Json;
    using System.Text.Json.Serialization;
    
    namespace Converter.shared
    {
      public class DateTimeConverter : JsonConverter<DateTime>
            {
                public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
                {
                    return DateTime.Parse(reader.GetString());
                }
    
                public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
                {
                    writer.WriteStringValue(value.ToLocalTime().ToString("yyyy-MM-ddTHH:mm:ss"));
                }
            }
     }
    

    【讨论】:

      猜你喜欢
      • 2019-09-22
      • 2014-04-04
      • 2016-10-23
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 2017-01-16
      相关资源
      最近更新 更多