utils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { createNamespace } from '../utils';
  2. var _createNamespace = createNamespace('calendar'),
  3. createComponent = _createNamespace[0],
  4. bem = _createNamespace[1],
  5. t = _createNamespace[2];
  6. export { createComponent, bem, t };
  7. export function formatMonthTitle(date) {
  8. return t('monthTitle', date.getFullYear(), date.getMonth() + 1);
  9. }
  10. export function compareMonth(date1, date2) {
  11. var year1 = date1.getFullYear();
  12. var year2 = date2.getFullYear();
  13. var month1 = date1.getMonth();
  14. var month2 = date2.getMonth();
  15. if (year1 === year2) {
  16. return month1 === month2 ? 0 : month1 > month2 ? 1 : -1;
  17. }
  18. return year1 > year2 ? 1 : -1;
  19. }
  20. export function compareDay(day1, day2) {
  21. var compareMonthResult = compareMonth(day1, day2);
  22. if (compareMonthResult === 0) {
  23. var date1 = day1.getDate();
  24. var date2 = day2.getDate();
  25. return date1 === date2 ? 0 : date1 > date2 ? 1 : -1;
  26. }
  27. return compareMonthResult;
  28. }
  29. export function getDayByOffset(date, offset) {
  30. date = new Date(date);
  31. date.setDate(date.getDate() + offset);
  32. return date;
  33. }
  34. export function getPrevDay(date) {
  35. return getDayByOffset(date, -1);
  36. }
  37. export function getNextDay(date) {
  38. return getDayByOffset(date, 1);
  39. }
  40. export function calcDateNum(date) {
  41. var day1 = date[0].getTime();
  42. var day2 = date[1].getTime();
  43. return (day2 - day1) / (1000 * 60 * 60 * 24) + 1;
  44. }
  45. export function copyDate(dates) {
  46. return new Date(dates);
  47. }
  48. export function copyDates(dates) {
  49. if (Array.isArray(dates)) {
  50. return dates.map(function (date) {
  51. if (date === null) {
  52. return date;
  53. }
  54. return copyDate(date);
  55. });
  56. }
  57. return copyDate(dates);
  58. }