【问题标题】:Date value gets saved differently when client and server time zones are different当客户端和服务器时区不同时,日期值的保存方式不同
【发布时间】:2018-08-31 14:04:59
【问题描述】:

我的 AngularJS 客户端以纪元毫秒为单位将日期值传递给使用 C#.NET 的服务器。我的客户端和服务器位于不同的时区。我从客户端传递日期值如下,它返回纪元毫秒:

var date = $scope.date.getTime()

如果我从客户那里选择了一个日期为“2018 年 1 月 16 日星期二 00:00:00 GMT+0530(印度标准时间)”,则纪元值对应到 1516041000000

但是当我将此纪元传递到我的服务器端时,GMT/UTC 时间,即 Monday, January 15, 2018 6:30:00 PM 正在保存到我的数据库。

我试图通过我的 API 标头将 GMT 偏移量传递给我的服务器,并将偏移量值添加到 UTC 时间。但这会在夏令时导致问题,因为不同日期值的偏移量不同。

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var savedDate = epoch.AddMilliseconds(long.Parse(passedDateEpoch)).AddMinutes(ClientGmtOffset);

有没有办法使用纪元时间值本身将输入的确切日期保存在服务器数据库中。我不希望将任何时间部分或不正确的日期保存到数据库中。

【问题讨论】:

    标签: javascript c# asp.net angularjs momentjs


    【解决方案1】:

    试试这个:

    double dateFromClient = double.Parse("1521707203364");
    var savedDate = (new DateTime(1970, 1, 1)).AddMilliseconds(dateFromClient);
    

    但我认为最好将 Unix 时间戳(例如 1521707203364)保存在数据库中,然后当您将其返回给客户端时,客户端会再次对其进行解析并获取正确的日期。

    【讨论】:

      【解决方案2】:

      您可以在服务器端使用以下代码

       public struct DateTimeWithZone
      {
          private readonly DateTime utcDateTime;
          private readonly TimeZoneInfo timeZone;
      
          public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone)
          {
              var dateTimeUnspec = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified);
              utcDateTime = TimeZoneInfo.ConvertTimeToUtc(dateTimeUnspec, timeZone); 
              this.timeZone = timeZone;
          }
      
          public DateTime UniversalTime { get { return utcDateTime; } }
      
          public TimeZoneInfo TimeZone { get { return timeZone; } }
      
          public DateTime LocalTime
          { 
              get 
              { 
                  return TimeZoneInfo.ConvertTime(utcDateTime, timeZone); 
              }
          }        
      }
      

      【讨论】:

        【解决方案3】:

        使用可以指定 webapiconfig 文件中未指定的时间更多信息 Like[keep C# datetime local time between json and Web api?

        您可以指定如下,以避免服务器端的时间转换

         public static void Register(HttpConfiguration config)
                {
                    // Web API configuration and services
        
                    var cors = new EnableCorsAttribute("*", "*", "*");
                    config.EnableCors(cors);
                    //subhash
                    log4net.Config.XmlConfigurator.Configure();
                    // Web API routes
                    config.MapHttpAttributeRoutes();
        
                    config.Routes.MapHttpRoute(
                        name: "DefaultApi",
                        routeTemplate: "api/{controller}/{action}/{id}",
                        defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional }
                    );
        
                    var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
                    jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        
        
                    config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Unspecified;
        
                    config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler());
                }
        

        希望对你有帮助

        【讨论】:

          【解决方案4】:

          这样从前端返回日期

          var date = new Date();

          var isoDate = date.toISOString()

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-07-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多