date_time_util.dart 5.3 KB

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