【问题标题】:Convert date time to C# timetick in Dart (Flutter) [closed]在 Dart (Flutter) 中将日期时间转换为 C# timetick [关闭]
【发布时间】:2021-07-07 13:11:32
【问题描述】:

我们如何将当前日期时间转换为颤振/飞镖中的时间?已经尝试了不同的公式和计算,没有奏效。 http://www.datetimetoticks-converter.com/

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我只是为了约会。

    您可以将 C# 中的实现复制到 dart 中。

    DateTime.DateToTicks(...)

    void main() {
      final dateTime = DateTime.now();
      print(DateTimeUtils.dateToTicks(
        dateTime.year,
        dateTime.month,
        dateTime.day,
      ));
    }
    class DateTimeUtils {
      static const int TicksPerMillisecond = 10000;
      static const int TicksPerSecond = TicksPerMillisecond * 1000;
      static const int TicksPerMinute = TicksPerSecond * 60;
      static const int TicksPerHour = TicksPerMinute * 60;
      static const int TicksPerDay = TicksPerHour * 24;
      static List<int> daysToMonth365 = [
        0,
        31,
        59,
        90,
        120,
        151,
        181,
        212,
        243,
        273,
        304,
        334,
        365,
      ];
      static List<int> daysToMonth366 = [
        0,
        31,
        60,
        91,
        121,
        152,
        182,
        213,
        244,
        274,
        305,
        335,
        366,
      ];
      static int dateToTicks(int year, int month, int day) {
        if (year >= 1 && year <= 9999 && month >= 1 && month <= 12) {
          List<int> days = isLeapYear(year) ? daysToMonth366 : daysToMonth365;
          if (day >= 1 && day <= days[month] - days[month - 1]) {
            int y = year - 1;
            double n = y * 365 + y / 4 - y / 100 + y / 400 + days[month - 1] + day - 1;
            return n * TicksPerDay as int;
          }
        }
        throw new ArgumentError();
      }
      static bool isLeapYear(int year) {
        if (year < 1 || year > 9999) {
          throw new ArgumentError();
        }
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
      }
    }
    

    【讨论】:

    • 谢谢,日期和时间呢?
    • 我认为是 DateToTicks + TimeToTicks。查看 C# 中的实现。
    猜你喜欢
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2022-01-23
    • 2019-02-22
    • 1970-01-01
    • 2012-08-03
    • 2019-04-11
    • 1970-01-01
    相关资源
    最近更新 更多