C#中如何将API响应的长整型时间戳转换为DateTime格式
在C#中解析毫秒级Unix时间戳为DateTime的方案
我来帮你搞定这个问题——你遇到的是毫秒级Unix时间戳转C# DateTime的场景,那些像1664496000000的键本质是从1970年1月1日UTC零点开始计算的毫秒数,对应你说的9/29/2022(UTC时间2022-09-29 00:00:00)。下面分两种场景给你具体代码实现:
一、单个时间戳直接转换
如果只是单独处理一个时间戳字符串,先把它转成long类型,再用.NET自带的方法转换:
适用于.NET Core 2.0+、.NET 5+、.NET Framework 4.6+
这些版本提供了直接的API,代码非常简洁:
// 把时间戳字符串转成long string timestampStr = "1664496000000"; if (long.TryParse(timestampStr, out long timestamp)) { // 转成UTC时间 DateTime utcDate = DateTimeOffset.FromUnixTimeMilliseconds(timestamp).UtcDateTime; // 转成本地时间(根据当前系统时区) DateTime localDate = DateTimeOffset.FromUnixTimeMilliseconds(timestamp).LocalDateTime; Console.WriteLine($"UTC时间: {utcDate:yyyy-MM-dd HH:mm:ss}"); Console.WriteLine($"本地时间: {localDate:yyyy-MM-dd HH:mm:ss}"); }
适用于.NET Framework 4.5及以下(老版本)
老版本没有FromUnixTimeMilliseconds方法,需要手动计算:
long timestamp = 1664496000000; // Unix纪元时间(1970-01-01 UTC) DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime utcDate = epoch.AddMilliseconds(timestamp); DateTime localDate = utcDate.ToLocalTime();
二、处理你给出的动态键JSON结构
你的JSON里键是动态的时间戳字符串,还有一个固定的index字段,这里推荐用两种常用的JSON库来解析:
方案1:使用Newtonsoft.Json(最流行的第三方库)
先通过NuGet安装Newtonsoft.Json包,然后用以下代码解析:
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; // 你的原始JSON字符串 string json = @"[ { ""1640908800000"": 123945000000, ""1648684800000"": 97278000000, ""1656547200000"": 82959000000, ""1664496000000"": 90146000000, ""index"": ""TableIndex"" }, { ""1640908800000"": 69702000000, ""1648684800000"": 54719000000, ""1656547200000"": 47074000000, ""1664496000000"": 52051000000, ""index"": ""BookIndex"" } ]"; // 解析成JSON数组 JArray dataArray = JArray.Parse(json); foreach (JObject item in dataArray) { // 获取index字段 string index = item["index"].ToString(); Console.WriteLine($"=== 当前索引: {index} ==="); // 遍历所有键,跳过index字段 foreach (var property in item.Properties()) { if (property.Name.Equals("index", StringComparison.OrdinalIgnoreCase)) continue; // 尝试把键转成时间戳 if (long.TryParse(property.Name, out long timestamp)) { DateTime date = DateTimeOffset.FromUnixTimeMilliseconds(timestamp).UtcDateTime; long value = (long)property.Value; Console.WriteLine($"日期: {date:yyyy-MM-dd}, 数值: {value}"); } } }
方案2:使用System.Text.Json(.NET Core 3.0+自带)
如果不想用第三方库,用.NET自带的System.Text.Json:
using System; using System.Text.Json; using System.Text.Json.Nodes; // 同样的JSON字符串 string json = @"[ { ""1640908800000"": 123945000000, ""1648684800000"": 97278000000, ""1656547200000"": 82959000000, ""1664496000000"": 90146000000, ""index"": ""TableIndex"" }, { ""1640908800000"": 69702000000, ""1648684800000"": 54719000000, ""1656547200000"": 47074000000, ""1664496000000"": 52051000000, ""index"": ""BookIndex"" } ]"; JsonArray dataArray = JsonSerializer.Deserialize<JsonArray>(json); foreach (JsonObject item in dataArray) { string index = item["index"]?.ToString() ?? string.Empty; Console.WriteLine($"=== 当前索引: {index} ==="); foreach (var (key, value) in item) { if (key.Equals("index", StringComparison.OrdinalIgnoreCase)) continue; if (long.TryParse(key, out long timestamp)) { DateTime date = DateTimeOffset.FromUnixTimeMilliseconds(timestamp).UtcDateTime; long numericValue = long.Parse(value.ToString()); Console.WriteLine($"日期: {date:yyyy-MM-dd}, 数值: {numericValue}"); } } }
注意事项
- 确保时间戳是有效的
long类型,用TryParse避免因格式错误抛出异常; - 这些时间戳是基于UTC的,转换时根据业务需求选择UTC时间或本地时间;
- 如果你的JSON结构固定但键动态,也可以用
Dictionary<string, object>来接收,再遍历字典处理。
内容的提问来源于stack exchange,提问作者FX_Sektor
相关产品推荐
相关产品推荐

