Angularjs 将时间从角度发送到.net web api时的时间更改
我正在将下面的对象从agular js(来自pickertime)发送到web apiAngularjs 将时间从角度发送到.net web api时的时间更改,angularjs,asp.net-web-api,Angularjs,Asp.net Web Api,我正在将下面的对象从agular js(来自pickertime)发送到web api Id : "14607" PartyId : "31558" Time : Thu Jan 01 1970 08:30:00 GMT-0800 (Pacific Standard Time) 但在我的网络api上它及时收到。。。(时间不同) 我如何解决这个问题以获得相同的时间 public class PersonalVM { public int? Id { get; set;
Id : "14607"
PartyId : "31558"
Time : Thu Jan 01 1970 08:30:00 GMT-0800 (Pacific Standard Time)
但在我的网络api上它及时收到。。。(时间不同)
我如何解决这个问题以获得相同的时间
public class PersonalVM
{
public int? Id { get; set; }
public int? PartyId { get; set; }
public DateTime? Time { get; set; }
public string Name { get; set; }
}
}
我按DateTimeOffset更改了DateTime,得到了相同的结果。解决方案是
时区.CurrentTimeZone.ToLocalTime(Model.Time.Value)。ToSortTimeString()
解决方案是时区.CurrentTimeZone.ToLocalTime(Model.Time.Value)。ToSortTimeString()
使用而不是字符串
假设您正在使用asp.net核心webapi。
我正在使用一个json转换器,它将unix时间戳转换为dateTime或Datetimeoffset,或将其转换为dateTime或Datetimeoffset
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
namespace JsonConverters
{
public class UnixDateTimeMilisecondsConverter : DateTimeConverterBase
{
internal static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
long milliseconds;
if (value is null)
{
milliseconds = 0;
}
else if (value is DateTime dateTime)
{
milliseconds = (long)(dateTime.ToUniversalTime() - UnixEpoch).TotalMilliseconds;
}
else if (value is DateTimeOffset dateTimeOffset)
{
milliseconds = dateTimeOffset.ToUnixTimeMilliseconds();
}
else
{
throw new JsonSerializationException("Expected date object value.");
}
if (milliseconds < 0)
{
// long? t = null;
// writer.WriteValue(t);
// return;
throw new JsonSerializationException("Cannot convert date value that is before Unix epoch of 00:00:00 UTC on 1 January 1970.");
}
writer.WriteValue(milliseconds);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
bool nullable = IsNullable(objectType);
if (reader.TokenType == JsonToken.Null)
{
if (!nullable)
{
throw new JsonSerializationException($"Cannot convert null value to {objectType}.");
}
return null;
}
long miliSeconds;
if (reader.TokenType == JsonToken.Integer)
{
miliSeconds = (long)reader.Value;
}
else if (reader.TokenType == JsonToken.String)
{
if (!long.TryParse((string)reader.Value, out miliSeconds))
{
throw new JsonSerializationException($"Cannot convert invalid value to {objectType}.");
}
}
else
{
throw new JsonSerializationException($"Unexpected token parsing date. Expected Integer or String, got {reader.TokenType}.");
}
if (miliSeconds >= 0)
{
DateTime d = UnixEpoch.AddMilliseconds(miliSeconds);
Type t = (nullable)
? Nullable.GetUnderlyingType(objectType)
: objectType;
if (t == typeof(DateTimeOffset))
{
return DateTimeOffset.FromUnixTimeMilliseconds(miliSeconds);
}
return d;
}
else
{
throw new JsonSerializationException($"Cannot convert value that is before Unix epoch of 00:00:00 UTC on 1 January 1970 to {0}. {objectType}.");
}
}
private static bool IsNullable(Type type)
{
return Nullable.GetUnderlyingType(type) != null;
}
}
}
现在在角度方面,你必须只发送或接收unix时间戳。
怎么做
使用Momnetjs作为客户端,让您的生活更轻松。使用代替字符串
假设您正在使用asp.net核心webapi。
我正在使用一个json转换器,它将unix时间戳转换为dateTime或Datetimeoffset,或将其转换为dateTime或Datetimeoffset
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
namespace JsonConverters
{
public class UnixDateTimeMilisecondsConverter : DateTimeConverterBase
{
internal static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
long milliseconds;
if (value is null)
{
milliseconds = 0;
}
else if (value is DateTime dateTime)
{
milliseconds = (long)(dateTime.ToUniversalTime() - UnixEpoch).TotalMilliseconds;
}
else if (value is DateTimeOffset dateTimeOffset)
{
milliseconds = dateTimeOffset.ToUnixTimeMilliseconds();
}
else
{
throw new JsonSerializationException("Expected date object value.");
}
if (milliseconds < 0)
{
// long? t = null;
// writer.WriteValue(t);
// return;
throw new JsonSerializationException("Cannot convert date value that is before Unix epoch of 00:00:00 UTC on 1 January 1970.");
}
writer.WriteValue(milliseconds);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
bool nullable = IsNullable(objectType);
if (reader.TokenType == JsonToken.Null)
{
if (!nullable)
{
throw new JsonSerializationException($"Cannot convert null value to {objectType}.");
}
return null;
}
long miliSeconds;
if (reader.TokenType == JsonToken.Integer)
{
miliSeconds = (long)reader.Value;
}
else if (reader.TokenType == JsonToken.String)
{
if (!long.TryParse((string)reader.Value, out miliSeconds))
{
throw new JsonSerializationException($"Cannot convert invalid value to {objectType}.");
}
}
else
{
throw new JsonSerializationException($"Unexpected token parsing date. Expected Integer or String, got {reader.TokenType}.");
}
if (miliSeconds >= 0)
{
DateTime d = UnixEpoch.AddMilliseconds(miliSeconds);
Type t = (nullable)
? Nullable.GetUnderlyingType(objectType)
: objectType;
if (t == typeof(DateTimeOffset))
{
return DateTimeOffset.FromUnixTimeMilliseconds(miliSeconds);
}
return d;
}
else
{
throw new JsonSerializationException($"Cannot convert value that is before Unix epoch of 00:00:00 UTC on 1 January 1970 to {0}. {objectType}.");
}
}
private static bool IsNullable(Type type)
{
return Nullable.GetUnderlyingType(type) != null;
}
}
}
现在在角度方面,你必须只发送或接收unix时间戳。
怎么做
在clinet端使用Momnetjs,让您的生活更轻松。这是因为当
Date
对象通过负载时,它会在将该对象发送到服务器之前执行序列化。。在序列化过程中,它试图通过从日期对象中添加/减去GMT组件,将日期转换为区域设置日期。。在这种情况下,如果您的日期为-8GMT,则它将在到达服务器时添加该时间,要解决此问题,您应该将日期存储到UTC TimeThank中。你告诉我解决方案是Model.Time.HasValue?TimeZone.CurrentTimeZone.ToLocalTime(Model.Time.Value).ToSortTimeString()我一直在做类似的事情。。。如果(candidate.DateOfBirth.HasValue)candidate.DateOfBirth=candidate.DateOfBirth.Value.ToLocalTime();这是因为当Date
对象通过有效负载时,它会在将该对象发送到服务器之前执行序列化。。在序列化过程中,它试图通过从日期对象中添加/减去GMT组件,将日期转换为区域设置日期。。在这种情况下,如果您的日期为-8GMT,则它将在到达服务器时添加该时间,要解决此问题,您应该将日期存储到UTC TimeThank中。你告诉我解决方案是Model.Time.HasValue?TimeZone.CurrentTimeZone.ToLocalTime(Model.Time.Value).ToSortTimeString()我一直在做类似的事情。。。如果(candidate.DateOfBirth.HasValue)candidate.DateOfBirth=candidate.DateOfBirth.Value.ToLocalTime();但这有点不方便。当API和UI托管在不同的服务器上时,此解决方案不起作用。当API和UI托管在不同的服务器上时,此解决方案不起作用。
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options =>
{
JsonSerializerSettings serializerSettings = options.SerializerSettings;
serializerSettings.Converters.Add(new UnixDateTimeMilisecondsConverter());
});