date_time_util.dart 5.8 KB

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