date_time_util.dart 5.1 KB

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