简体   繁体   English

从json到C#的datetime格式

[英]datetime format from json to C#

I'm trying to parse the json data sent from stripe payment gateway, and the json data have the datetime encoded as a number. 我正在尝试解析从条带支付网关发送的json数据,并且json数据将日期时间编码为数字。 I tried to parse it into a proper .NET DateTime, but didnt succeed. 我试图将其解析为正确的.NET DateTime,但没有成功。

Can you please let me know how ? 你能告诉我怎么样吗?

Reproduced the json below. 转载下面的json。

在此输入图像描述

In C# DateTimeOffset has FromUnixTimeSeconds : 在C# DateTimeOffsetFromUnixTimeSeconds

// converts to UTC DateTimeOffset
var dtOffset = DateTimeOffset.FromUnixTimeSeconds(1530291339); 

// if you need a DateTime you can
var dt = dtOffset.UtcDateTime;

// dtOffset.ToString() for example above:
// 6/29/2018 4:55:39 PM 

DateTimeOffset also has FromUnixTimeMilliseconds method. DateTimeOffset还具有FromUnixTimeMilliseconds方法。 Please see DateTimeOffset for more info. 有关详细信息,请参阅DateTimeOffset

The methods FromUnixTimeSeconds and FromUnixTimeMilliseconds convert the UNIX timestamp (since 01/01/1070) date to a UTC DateTimeOffset. FromUnixTimeSecondsFromUnixTimeMilliseconds方法将UNIX时间戳(自01/01/1070)日期转换为UTC DateTimeOffset。

The Offset property value of the returned DateTimeOffset instance is TimeSpan.Zero, which represents Coordinated Universal Time. 返回的DateTimeOffset实例的Offset属性值是TimeSpan.Zero,表示协调世界时。

It can be converted to the time in a specific time zone by calling the TimeZoneInfo.ConvertTime() method. 可以通过调用TimeZoneInfo.ConvertTime()方法将其转换为特定时区的时间。

That's just the time stamp. 那只是时间戳。 Simply parse the date by doing new Date(<timestamp>) 只需通过执行new Date(<timestamp>)解析日期

 const date = new Date().getTime(); console.log(date); // <-- your number console.log(new Date(date)); // <-- convert the timestamp to a date 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime

https://stripe.com/docs/api/skus/object?lang=dotnet#sku_object-created https://stripe.com/docs/api/skus/object?lang=dotnet#sku_object-created

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM