date_time_util.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import 'package:flutter/material.dart';
  2. Map<int, String> _months = {
  3. 1: "Jan",
  4. 2: "Feb",
  5. 3: "March",
  6. 4: "April",
  7. 5: "May",
  8. 6: "Jun",
  9. 7: "July",
  10. 8: "Aug",
  11. 9: "Sep",
  12. 10: "Oct",
  13. 11: "Nov",
  14. 12: "Dec",
  15. };
  16. Map<int, String> _fullMonths = {
  17. 1: "January",
  18. 2: "February",
  19. 3: "March",
  20. 4: "April",
  21. 5: "May",
  22. 6: "June",
  23. 7: "July",
  24. 8: "August",
  25. 9: "September",
  26. 10: "October",
  27. 11: "November",
  28. 12: "December",
  29. };
  30. Map<int, String> _days = {
  31. 1: "Mon",
  32. 2: "Tue",
  33. 3: "Wed",
  34. 4: "Thu",
  35. 5: "Fri",
  36. 6: "Sat",
  37. 7: "Sun",
  38. };
  39. String getMonthAndYear(DateTime dateTime) {
  40. return _months[dateTime.month] + " " + dateTime.year.toString();
  41. }
  42. String getDayAndMonth(DateTime dateTime) {
  43. return _days[dateTime.weekday] +
  44. ", " +
  45. dateTime.day.toString() +
  46. " " +
  47. _months[dateTime.month];
  48. }
  49. String getDateAndMonthAndYear(DateTime dateTime) {
  50. return dateTime.day.toString() +
  51. " " +
  52. _months[dateTime.month] +
  53. ", " +
  54. dateTime.year.toString();
  55. }
  56. String getDay(DateTime dateTime) {
  57. return _days[dateTime.weekday];
  58. }
  59. String getMonth(DateTime dateTime) {
  60. return _months[dateTime.month];
  61. }
  62. String getFullMonth(DateTime dateTime) {
  63. return _fullMonths[dateTime.month];
  64. }
  65. String getAbbreviationOfYear(DateTime dateTime) {
  66. return (dateTime.year % 100).toString();
  67. }
  68. String getTime(DateTime dateTime) {
  69. final hours = dateTime.hour > 9
  70. ? dateTime.hour.toString()
  71. : "0" + dateTime.hour.toString();
  72. final minutes = dateTime.minute > 9
  73. ? dateTime.minute.toString()
  74. : "0" + dateTime.minute.toString();
  75. return hours + ":" + minutes;
  76. }
  77. String getFormattedTime(DateTime dateTime) {
  78. return getDay(dateTime) +
  79. ", " +
  80. getMonth(dateTime) +
  81. " " +
  82. dateTime.day.toString() +
  83. ", " +
  84. dateTime.year.toString() +
  85. " - " +
  86. getTime(dateTime);
  87. }
  88. String getFormattedDate(DateTime dateTime) {
  89. return dateTime.day.toString() +
  90. " " +
  91. getMonth(dateTime) +
  92. "'" +
  93. getAbbreviationOfYear(dateTime);
  94. }
  95. String daysLeft(int futureTime) {
  96. int daysLeft = ((futureTime - DateTime.now().microsecondsSinceEpoch) /
  97. Duration.microsecondsPerDay)
  98. .ceil();
  99. return '$daysLeft day' + (daysLeft <= 1 ? "" : "s");
  100. }
  101. String formatDuration(Duration position) {
  102. final ms = position.inMilliseconds;
  103. int seconds = ms ~/ 1000;
  104. final int hours = seconds ~/ 3600;
  105. seconds = seconds % 3600;
  106. var minutes = seconds ~/ 60;
  107. seconds = seconds % 60;
  108. final hoursString = hours >= 10
  109. ? '$hours'
  110. : hours == 0
  111. ? '00'
  112. : '0$hours';
  113. final minutesString = minutes >= 10
  114. ? '$minutes'
  115. : minutes == 0
  116. ? '00'
  117. : '0$minutes';
  118. final secondsString = seconds >= 10
  119. ? '$seconds'
  120. : seconds == 0
  121. ? '00'
  122. : '0$seconds';
  123. final formattedTime =
  124. '${hoursString == '00' ? '' : hoursString + ':'}$minutesString:$secondsString';
  125. return formattedTime;
  126. }
  127. bool isLeapYear(DateTime dateTime) {
  128. final year = dateTime.year;
  129. if (year % 4 == 0) {
  130. if (year % 100 == 0) {
  131. if (year % 400 == 0) {
  132. return true;
  133. } else {
  134. return false;
  135. }
  136. } else {
  137. return true;
  138. }
  139. } else {
  140. return false;
  141. }
  142. }
  143. Widget getDayWidget(
  144. BuildContext context, int timestamp, bool smallerTodayFont) {
  145. return Container(
  146. padding: const EdgeInsets.fromLTRB(4, 14, 0, 8),
  147. alignment: Alignment.centerLeft,
  148. child: Text(
  149. getDayTitle(timestamp),
  150. style: (getDayTitle(timestamp) == "Today" && !smallerTodayFont)
  151. ? Theme.of(context).textTheme.headline5
  152. : Theme.of(context).textTheme.caption.copyWith(
  153. fontSize: 16,
  154. fontWeight: FontWeight.w600,
  155. fontFamily: 'Inter-SemiBold'),
  156. ),
  157. );
  158. }
  159. String getDayTitle(int timestamp) {
  160. final date = DateTime.fromMicrosecondsSinceEpoch(timestamp);
  161. final now = DateTime.now();
  162. var title = getDayAndMonth(date);
  163. if (date.year == now.year && date.month == now.month) {
  164. if (date.day == now.day) {
  165. title = "Today";
  166. } else if (date.day == now.day - 1) {
  167. title = "Yesterday";
  168. }
  169. }
  170. if (date.year != DateTime.now().year) {
  171. title += " " + date.year.toString();
  172. }
  173. return title;
  174. }