time.dart 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /// 时间工具类
  2. class TimeUtils {
  3. /// 获取现在的时间
  4. static int getDayNow() {
  5. var nowTime = DateTime.now();
  6. return nowTime.millisecondsSinceEpoch;
  7. }
  8. /// 获取今天的开始时间
  9. static int getDayBegin() {
  10. var nowTime = DateTime.now();
  11. var day = new DateTime(nowTime.year, nowTime.month, nowTime.day, 0, 0, 0);
  12. return day.millisecondsSinceEpoch;
  13. }
  14. /// 获取昨天的开始时间
  15. static int getBeginDayOfYesterday() {
  16. var nowTime = DateTime.now();
  17. var yesterday = nowTime.add(new Duration(days: -1));
  18. var day =
  19. new DateTime(yesterday.year, yesterday.month, yesterday.day, 0, 0, 0);
  20. return day.millisecondsSinceEpoch;
  21. }
  22. /// 获取昨天的结束时间
  23. static int getEndDayOfYesterDay() {
  24. var nowTime = DateTime.now();
  25. var yesterday = nowTime.add(new Duration(days: -1));
  26. var day = new DateTime(
  27. yesterday.year, yesterday.month, yesterday.day, 23, 59, 59);
  28. return day.millisecondsSinceEpoch;
  29. }
  30. /// 获取本周的开始时间
  31. static int getBeginDayOfWeek() {
  32. var nowTime = DateTime.now();
  33. var weekday = nowTime.weekday;
  34. var yesterday = nowTime.add(new Duration(days: -(weekday - 1)));
  35. var day =
  36. new DateTime(yesterday.year, yesterday.month, yesterday.day, 0, 0, 0);
  37. return day.millisecondsSinceEpoch;
  38. }
  39. /// 获取本月的开始时间
  40. static int getBeginDayOfMonth() {
  41. var nowTime = DateTime.now();
  42. var day = new DateTime(nowTime.year, nowTime.month, 1, 0, 0, 0);
  43. return day.millisecondsSinceEpoch;
  44. }
  45. /// 获取本年的开始时间
  46. static int getBeginDayOfYear() {
  47. var nowTime = DateTime.now();
  48. var day = new DateTime(nowTime.year, 1, 1, 0, 0, 0);
  49. return day.millisecondsSinceEpoch;
  50. }
  51. }