将字节数组转换为日期时间

问题描述 投票:0回答:1

我有下面的字节数组和时间值: 字节:00 05 fc 3b d1 bb c0 c2 时间:2023年5月22日 5:38:55 上午 字节:00 06 07 04 94 65 f7 81 时间: 10/06/2023 11:21:51 am 我找不到将此字节 arr 转换为日期时间的解决方案。我尝试使用这段代码,但事实并非如此:

如何在 C# 中将 byte[] 转换为日期时间?

请帮助我!谢谢你

如何在 C# 中将 byte[] 转换为日期时间?

c# datetime-format
1个回答
0
投票

我猜

2023-05-22 05:38:55
2023-05-21 22:38:55
之间的区别是时区;大概距 UTC 时间还有 7 小时。您需要在当地进行补偿。

using System;
using System.Buffers.Binary;

byte[] bytes = [0x00, 0x05, 0xfc, 0x3b, 0xd1, 0xbb, 0xc0, 0xc2];
var unixMicros = BinaryPrimitives.ReadInt64BigEndian(bytes);

var when = UnixTime.Epoch.AddMicroseconds(unixMicros);
Console.WriteLine(when); // 21/05/2023 22:38:55

internal static class UnixTime
{
    public static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
}
© www.soinside.com 2019 - 2024. All rights reserved.