You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中如何用反向公式将考勤控制器数值时间转换为正常DateTime

Controller DT 转标准日期 C# 实现方案

转换逻辑说明

原设备的时间编码规则固定按每年12个月、每月31天计算,不考虑实际公历的月份天数差异,反向转换严格遵循该规则即可。

完整代码实现

using System;

public static class ControllerTimeConverter
{
    private const int SecondsPerDay = 24 * 60 * 60;
    private const int DaysPerMonth = 31;
    private const int MonthsPerYear = 12;
    private const int BaseYear = 2000;

    public static DateTime ConvertControllerDtToDateTime(long controllerDt)
    {
        if (controllerDt < 0)
            throw new ArgumentOutOfRangeException(nameof(controllerDt), "Controller DT 不能为负数");

        // 拆分总秒数为总天数和当日剩余秒数
        long totalDays = controllerDt / SecondsPerDay;
        int remainingSeconds = (int)(controllerDt % SecondsPerDay);

        // 计算时分秒
        int hour = remainingSeconds / 3600;
        remainingSeconds %= 3600;
        int minute = remainingSeconds / 60;
        int second = remainingSeconds % 60;

        // 按设备规则计算年月日(固定每年372天、每月31天)
        int year = BaseYear + (int)(totalDays / (MonthsPerYear * DaysPerMonth));
        int remainingDays = (int)(totalDays % (MonthsPerYear * DaysPerMonth));
        int month = 1 + (remainingDays / DaysPerMonth);
        int day = 1 + (remainingDays % DaysPerMonth);

        return new DateTime(year, month, day, hour, minute, second);
    }

    // 测试示例
    public static void Main()
    {
        long testDt = 690590751;
        DateTime result = ConvertControllerDtToDateTime(testDt);
        Console.WriteLine(result.ToString("yyyy-MM-dd HH:mm:ss.fff"));
        // 输出:2021-06-26 22:45:51.000
    }
}

验证说明

你可以直接用示例中的数值690590751测试,输出结果和你提供的对照表完全匹配,其余对照表内的数值也可通过该方法正确转换。

内容的提问来源于stack exchange,提问作者MaheshBabu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 06:42:03