date_time_util.dart 5.2 KB

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