date-util.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.validateRangeInOneMonth = exports.extractTimeFormat = exports.extractDateFormat = exports.nextYear = exports.prevYear = exports.nextMonth = exports.prevMonth = exports.changeYearMonthAndClampDate = exports.timeWithinRange = exports.limitTimeRange = exports.clearMilliseconds = exports.clearTime = exports.modifyWithTimeString = exports.modifyTime = exports.modifyDate = exports.range = exports.getRangeMinutes = exports.getMonthDays = exports.getPrevMonthLastDays = exports.getRangeHours = exports.getWeekNumber = exports.getStartDateOfMonth = exports.nextDate = exports.prevDate = exports.getFirstDayOfMonth = exports.getDayCountOfYear = exports.getDayCountOfMonth = exports.parseDate = exports.formatDate = exports.isDateObject = exports.isDate = exports.toDate = exports.getI18nSettings = undefined;
  4. var _date = require('element-ui/lib/utils/date');
  5. var _date2 = _interopRequireDefault(_date);
  6. var _locale = require('element-ui/lib/locale');
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var weeks = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
  9. var months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
  10. var newArray = function newArray(start, end) {
  11. var result = [];
  12. for (var i = start; i <= end; i++) {
  13. result.push(i);
  14. }
  15. return result;
  16. };
  17. var getI18nSettings = exports.getI18nSettings = function getI18nSettings() {
  18. return {
  19. dayNamesShort: weeks.map(function (week) {
  20. return (0, _locale.t)('el.datepicker.weeks.' + week);
  21. }),
  22. dayNames: weeks.map(function (week) {
  23. return (0, _locale.t)('el.datepicker.weeks.' + week);
  24. }),
  25. monthNamesShort: months.map(function (month) {
  26. return (0, _locale.t)('el.datepicker.months.' + month);
  27. }),
  28. monthNames: months.map(function (month, index) {
  29. return (0, _locale.t)('el.datepicker.month' + (index + 1));
  30. }),
  31. amPm: ['am', 'pm']
  32. };
  33. };
  34. var toDate = exports.toDate = function toDate(date) {
  35. return isDate(date) ? new Date(date) : null;
  36. };
  37. var isDate = exports.isDate = function isDate(date) {
  38. if (date === null || date === undefined) return false;
  39. if (isNaN(new Date(date).getTime())) return false;
  40. if (Array.isArray(date)) return false; // deal with `new Date([ new Date() ]) -> new Date()`
  41. return true;
  42. };
  43. var isDateObject = exports.isDateObject = function isDateObject(val) {
  44. return val instanceof Date;
  45. };
  46. var formatDate = exports.formatDate = function formatDate(date, format) {
  47. date = toDate(date);
  48. if (!date) return '';
  49. return _date2.default.format(date, format || 'yyyy-MM-dd', getI18nSettings());
  50. };
  51. var parseDate = exports.parseDate = function parseDate(string, format) {
  52. return _date2.default.parse(string, format || 'yyyy-MM-dd', getI18nSettings());
  53. };
  54. var getDayCountOfMonth = exports.getDayCountOfMonth = function getDayCountOfMonth(year, month) {
  55. if (month === 3 || month === 5 || month === 8 || month === 10) {
  56. return 30;
  57. }
  58. if (month === 1) {
  59. if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
  60. return 29;
  61. } else {
  62. return 28;
  63. }
  64. }
  65. return 31;
  66. };
  67. var getDayCountOfYear = exports.getDayCountOfYear = function getDayCountOfYear(year) {
  68. var isLeapYear = year % 400 === 0 || year % 100 !== 0 && year % 4 === 0;
  69. return isLeapYear ? 366 : 365;
  70. };
  71. var getFirstDayOfMonth = exports.getFirstDayOfMonth = function getFirstDayOfMonth(date) {
  72. var temp = new Date(date.getTime());
  73. temp.setDate(1);
  74. return temp.getDay();
  75. };
  76. // see: https://stackoverflow.com/questions/3674539/incrementing-a-date-in-javascript
  77. // {prev, next} Date should work for Daylight Saving Time
  78. // Adding 24 * 60 * 60 * 1000 does not work in the above scenario
  79. var prevDate = exports.prevDate = function prevDate(date) {
  80. var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  81. return new Date(date.getFullYear(), date.getMonth(), date.getDate() - amount);
  82. };
  83. var nextDate = exports.nextDate = function nextDate(date) {
  84. var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  85. return new Date(date.getFullYear(), date.getMonth(), date.getDate() + amount);
  86. };
  87. var getStartDateOfMonth = exports.getStartDateOfMonth = function getStartDateOfMonth(year, month) {
  88. var result = new Date(year, month, 1);
  89. var day = result.getDay();
  90. if (day === 0) {
  91. return prevDate(result, 7);
  92. } else {
  93. return prevDate(result, day);
  94. }
  95. };
  96. var getWeekNumber = exports.getWeekNumber = function getWeekNumber(src) {
  97. if (!isDate(src)) return null;
  98. var date = new Date(src.getTime());
  99. date.setHours(0, 0, 0, 0);
  100. // Thursday in current week decides the year.
  101. date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  102. // January 4 is always in week 1.
  103. var week1 = new Date(date.getFullYear(), 0, 4);
  104. // Adjust to Thursday in week 1 and count number of weeks from date to week 1.
  105. // Rounding should be fine for Daylight Saving Time. Its shift should never be more than 12 hours.
  106. return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
  107. };
  108. var getRangeHours = exports.getRangeHours = function getRangeHours(ranges) {
  109. var hours = [];
  110. var disabledHours = [];
  111. (ranges || []).forEach(function (range) {
  112. var value = range.map(function (date) {
  113. return date.getHours();
  114. });
  115. disabledHours = disabledHours.concat(newArray(value[0], value[1]));
  116. });
  117. if (disabledHours.length) {
  118. for (var i = 0; i < 24; i++) {
  119. hours[i] = disabledHours.indexOf(i) === -1;
  120. }
  121. } else {
  122. for (var _i = 0; _i < 24; _i++) {
  123. hours[_i] = false;
  124. }
  125. }
  126. return hours;
  127. };
  128. var getPrevMonthLastDays = exports.getPrevMonthLastDays = function getPrevMonthLastDays(date, amount) {
  129. if (amount <= 0) return [];
  130. var temp = new Date(date.getTime());
  131. temp.setDate(0);
  132. var lastDay = temp.getDate();
  133. return range(amount).map(function (_, index) {
  134. return lastDay - (amount - index - 1);
  135. });
  136. };
  137. var getMonthDays = exports.getMonthDays = function getMonthDays(date) {
  138. var temp = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  139. var days = temp.getDate();
  140. return range(days).map(function (_, index) {
  141. return index + 1;
  142. });
  143. };
  144. function setRangeData(arr, start, end, value) {
  145. for (var i = start; i < end; i++) {
  146. arr[i] = value;
  147. }
  148. }
  149. var getRangeMinutes = exports.getRangeMinutes = function getRangeMinutes(ranges, hour) {
  150. var minutes = new Array(60);
  151. if (ranges.length > 0) {
  152. ranges.forEach(function (range) {
  153. var start = range[0];
  154. var end = range[1];
  155. var startHour = start.getHours();
  156. var startMinute = start.getMinutes();
  157. var endHour = end.getHours();
  158. var endMinute = end.getMinutes();
  159. if (startHour === hour && endHour !== hour) {
  160. setRangeData(minutes, startMinute, 60, true);
  161. } else if (startHour === hour && endHour === hour) {
  162. setRangeData(minutes, startMinute, endMinute + 1, true);
  163. } else if (startHour !== hour && endHour === hour) {
  164. setRangeData(minutes, 0, endMinute + 1, true);
  165. } else if (startHour < hour && endHour > hour) {
  166. setRangeData(minutes, 0, 60, true);
  167. }
  168. });
  169. } else {
  170. setRangeData(minutes, 0, 60, true);
  171. }
  172. return minutes;
  173. };
  174. var range = exports.range = function range(n) {
  175. // see https://stackoverflow.com/questions/3746725/create-a-javascript-array-containing-1-n
  176. return Array.apply(null, { length: n }).map(function (_, n) {
  177. return n;
  178. });
  179. };
  180. var modifyDate = exports.modifyDate = function modifyDate(date, y, m, d) {
  181. return new Date(y, m, d, date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
  182. };
  183. var modifyTime = exports.modifyTime = function modifyTime(date, h, m, s) {
  184. return new Date(date.getFullYear(), date.getMonth(), date.getDate(), h, m, s, date.getMilliseconds());
  185. };
  186. var modifyWithTimeString = exports.modifyWithTimeString = function modifyWithTimeString(date, time) {
  187. if (date == null || !time) {
  188. return date;
  189. }
  190. time = parseDate(time, 'HH:mm:ss');
  191. return modifyTime(date, time.getHours(), time.getMinutes(), time.getSeconds());
  192. };
  193. var clearTime = exports.clearTime = function clearTime(date) {
  194. return new Date(date.getFullYear(), date.getMonth(), date.getDate());
  195. };
  196. var clearMilliseconds = exports.clearMilliseconds = function clearMilliseconds(date) {
  197. return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), 0);
  198. };
  199. var limitTimeRange = exports.limitTimeRange = function limitTimeRange(date, ranges) {
  200. var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'HH:mm:ss';
  201. // TODO: refactory a more elegant solution
  202. if (ranges.length === 0) return date;
  203. var normalizeDate = function normalizeDate(date) {
  204. return _date2.default.parse(_date2.default.format(date, format), format);
  205. };
  206. var ndate = normalizeDate(date);
  207. var nranges = ranges.map(function (range) {
  208. return range.map(normalizeDate);
  209. });
  210. if (nranges.some(function (nrange) {
  211. return ndate >= nrange[0] && ndate <= nrange[1];
  212. })) return date;
  213. var minDate = nranges[0][0];
  214. var maxDate = nranges[0][0];
  215. nranges.forEach(function (nrange) {
  216. minDate = new Date(Math.min(nrange[0], minDate));
  217. maxDate = new Date(Math.max(nrange[1], minDate));
  218. });
  219. var ret = ndate < minDate ? minDate : maxDate;
  220. // preserve Year/Month/Date
  221. return modifyDate(ret, date.getFullYear(), date.getMonth(), date.getDate());
  222. };
  223. var timeWithinRange = exports.timeWithinRange = function timeWithinRange(date, selectableRange, format) {
  224. var limitedDate = limitTimeRange(date, selectableRange, format);
  225. return limitedDate.getTime() === date.getTime();
  226. };
  227. var changeYearMonthAndClampDate = exports.changeYearMonthAndClampDate = function changeYearMonthAndClampDate(date, year, month) {
  228. // clamp date to the number of days in `year`, `month`
  229. // eg: (2010-1-31, 2010, 2) => 2010-2-28
  230. var monthDate = Math.min(date.getDate(), getDayCountOfMonth(year, month));
  231. return modifyDate(date, year, month, monthDate);
  232. };
  233. var prevMonth = exports.prevMonth = function prevMonth(date) {
  234. var year = date.getFullYear();
  235. var month = date.getMonth();
  236. return month === 0 ? changeYearMonthAndClampDate(date, year - 1, 11) : changeYearMonthAndClampDate(date, year, month - 1);
  237. };
  238. var nextMonth = exports.nextMonth = function nextMonth(date) {
  239. var year = date.getFullYear();
  240. var month = date.getMonth();
  241. return month === 11 ? changeYearMonthAndClampDate(date, year + 1, 0) : changeYearMonthAndClampDate(date, year, month + 1);
  242. };
  243. var prevYear = exports.prevYear = function prevYear(date) {
  244. var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  245. var year = date.getFullYear();
  246. var month = date.getMonth();
  247. return changeYearMonthAndClampDate(date, year - amount, month);
  248. };
  249. var nextYear = exports.nextYear = function nextYear(date) {
  250. var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  251. var year = date.getFullYear();
  252. var month = date.getMonth();
  253. return changeYearMonthAndClampDate(date, year + amount, month);
  254. };
  255. var extractDateFormat = exports.extractDateFormat = function extractDateFormat(format) {
  256. return format.replace(/\W?m{1,2}|\W?ZZ/g, '').replace(/\W?h{1,2}|\W?s{1,3}|\W?a/gi, '').trim();
  257. };
  258. var extractTimeFormat = exports.extractTimeFormat = function extractTimeFormat(format) {
  259. return format.replace(/\W?D{1,2}|\W?Do|\W?d{1,4}|\W?M{1,4}|\W?y{2,4}/g, '').trim();
  260. };
  261. var validateRangeInOneMonth = exports.validateRangeInOneMonth = function validateRangeInOneMonth(start, end) {
  262. return start.getMonth() === end.getMonth() && start.getFullYear() === end.getFullYear();
  263. };