简体   繁体   中英

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. I tried to parse it into a proper .NET DateTime, but didnt succeed.

Can you please let me know how ?

Reproduced the json below.

在此输入图像描述

In C# DateTimeOffset has FromUnixTimeSeconds :

// 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. Please see DateTimeOffset for more info.

The methods FromUnixTimeSeconds and FromUnixTimeMilliseconds convert the UNIX timestamp (since 01/01/1070) date to a UTC DateTimeOffset.

The Offset property value of the returned DateTimeOffset instance is TimeSpan.Zero, which represents Coordinated Universal Time.

It can be converted to the time in a specific time zone by calling the TimeZoneInfo.ConvertTime() method.

That's just the time stamp. Simply parse the date by doing 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://stripe.com/docs/api/skus/object?lang=dotnet#sku_object-created

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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