date_time_util.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 getTime(DateTime dateTime) {
  66. final hours = dateTime.hour > 9
  67. ? dateTime.hour.toString()
  68. : "0" + dateTime.hour.toString();
  69. final minutes = dateTime.minute > 9
  70. ? dateTime.minute.toString()
  71. : "0" + dateTime.minute.toString();
  72. return hours + ":" + minutes;
  73. }
  74. String getFormattedTime(DateTime dateTime) {
  75. return getDay(dateTime) +
  76. ", " +
  77. getMonth(dateTime) +
  78. " " +
  79. dateTime.day.toString() +
  80. ", " +
  81. dateTime.year.toString() +
  82. " - " +
  83. getTime(dateTime);
  84. }
  85. String getFormattedDate(DateTime dateTime) {
  86. return getDay(dateTime) +
  87. ", " +
  88. getMonth(dateTime) +
  89. " " +
  90. dateTime.day.toString() +
  91. ", " +
  92. dateTime.year.toString();
  93. }
  94. String daysLeft(int futureTime) {
  95. int daysLeft = ((futureTime - DateTime.now().microsecondsSinceEpoch) /
  96. Duration.microsecondsPerDay)
  97. .ceil();
  98. return '$daysLeft day' + (daysLeft <= 1 ? "" : "s");
  99. }
  100. String formatDuration(Duration position) {
  101. final ms = position.inMilliseconds;
  102. int seconds = ms ~/ 1000;
  103. final int hours = seconds ~/ 3600;
  104. seconds = seconds % 3600;
  105. var minutes = seconds ~/ 60;
  106. seconds = seconds % 60;
  107. final hoursString = hours >= 10
  108. ? '$hours'
  109. : hours == 0
  110. ? '00'
  111. : '0$hours';
  112. final minutesString = minutes >= 10
  113. ? '$minutes'
  114. : minutes == 0
  115. ? '00'
  116. : '0$minutes';
  117. final secondsString = seconds >= 10
  118. ? '$seconds'
  119. : seconds == 0
  120. ? '00'
  121. : '0$seconds';
  122. final formattedTime =
  123. '${hoursString == '00' ? '' : hoursString + ':'}$minutesString:$secondsString';
  124. return formattedTime;
  125. }
  126. bool isLeapYear(DateTime dateTime) {
  127. final year = dateTime.year;
  128. if (year % 4 == 0) {
  129. if (year % 100 == 0) {
  130. if (year % 400 == 0) {
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. } else {
  136. return true;
  137. }
  138. } else {
  139. return false;
  140. }
  141. }
  142. Widget getDayWidget(BuildContext context, int timestamp) {
  143. return Container(
  144. padding: const EdgeInsets.fromLTRB(10, 14, 0, 12),
  145. alignment: Alignment.centerLeft,
  146. child: Text(getDayTitle(timestamp),
  147. style: getDayTitle(timestamp) == "Today"
  148. ? Theme.of(context).textTheme.headline5
  149. : Theme.of(context).textTheme.caption.copyWith(
  150. fontSize: 16,
  151. fontWeight: FontWeight.w600,
  152. fontFamily: 'Inter-SemiBold')));
  153. }
  154. String getDayTitle(int timestamp) {
  155. final date = DateTime.fromMicrosecondsSinceEpoch(timestamp);
  156. final now = DateTime.now();
  157. var title = getDayAndMonth(date);
  158. if (date.year == now.year && date.month == now.month) {
  159. if (date.day == now.day) {
  160. title = "Today";
  161. } else if (date.day == now.day - 1) {
  162. title = "Yesterday";
  163. }
  164. }
  165. if (date.year != DateTime.now().year) {
  166. title += " " + date.year.toString();
  167. }
  168. return title;
  169. }