dayjs.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import dayjs from 'dayjs';
  2. import weekday from 'dayjs/plugin/weekday';
  3. import localeData from 'dayjs/plugin/localeData';
  4. import weekOfYear from 'dayjs/plugin/weekOfYear';
  5. import weekYear from 'dayjs/plugin/weekYear';
  6. import advancedFormat from 'dayjs/plugin/advancedFormat';
  7. import customParseFormat from 'dayjs/plugin/customParseFormat';
  8. import { noteOnce } from '../../vc-util/warning';
  9. dayjs.extend(customParseFormat);
  10. dayjs.extend(advancedFormat);
  11. dayjs.extend(weekday);
  12. dayjs.extend(localeData);
  13. dayjs.extend(weekOfYear);
  14. dayjs.extend(weekYear);
  15. dayjs.extend(function (_o, c) {
  16. // todo support Wo (ISO week)
  17. var proto = c.prototype;
  18. var oldFormat = proto.format;
  19. proto.format = function f(formatStr) {
  20. var str = (formatStr || '').replace('Wo', 'wo');
  21. return oldFormat.bind(this)(str);
  22. };
  23. });
  24. var localeMap = {
  25. // ar_EG:
  26. // az_AZ:
  27. // bg_BG:
  28. bn_BD: 'bn-bd',
  29. by_BY: 'be',
  30. // ca_ES:
  31. // cs_CZ:
  32. // da_DK:
  33. // de_DE:
  34. // el_GR:
  35. en_GB: 'en-gb',
  36. en_US: 'en',
  37. // es_ES:
  38. // et_EE:
  39. // fa_IR:
  40. // fi_FI:
  41. fr_BE: 'fr',
  42. fr_CA: 'fr-ca',
  43. // fr_FR:
  44. // ga_IE:
  45. // gl_ES:
  46. // he_IL:
  47. // hi_IN:
  48. // hr_HR:
  49. // hu_HU:
  50. hy_AM: 'hy-am',
  51. // id_ID:
  52. // is_IS:
  53. // it_IT:
  54. // ja_JP:
  55. // ka_GE:
  56. // kk_KZ:
  57. // km_KH:
  58. kmr_IQ: 'ku',
  59. // kn_IN:
  60. // ko_KR:
  61. // ku_IQ: // previous ku in antd
  62. // lt_LT:
  63. // lv_LV:
  64. // mk_MK:
  65. // ml_IN:
  66. // mn_MN:
  67. // ms_MY:
  68. // nb_NO:
  69. // ne_NP:
  70. nl_BE: 'nl-be',
  71. // nl_NL:
  72. // pl_PL:
  73. pt_BR: 'pt-br',
  74. // pt_PT:
  75. // ro_RO:
  76. // ru_RU:
  77. // sk_SK:
  78. // sl_SI:
  79. // sr_RS:
  80. // sv_SE:
  81. // ta_IN:
  82. // th_TH:
  83. // tr_TR:
  84. // uk_UA:
  85. // ur_PK:
  86. // vi_VN:
  87. zh_CN: 'zh-cn',
  88. zh_HK: 'zh-hk',
  89. zh_TW: 'zh-tw'
  90. };
  91. var parseLocale = function parseLocale(locale) {
  92. var mapLocale = localeMap[locale];
  93. return mapLocale || locale.split('_')[0];
  94. };
  95. var parseNoMatchNotice = function parseNoMatchNotice() {
  96. /* istanbul ignore next */
  97. noteOnce(false, 'Not match any format. Please help to fire a issue about this.');
  98. };
  99. var generateConfig = {
  100. // get
  101. getNow: function getNow() {
  102. return dayjs();
  103. },
  104. getFixedDate: function getFixedDate(string) {
  105. return dayjs(string, ['YYYY-M-DD', 'YYYY-MM-DD']);
  106. },
  107. getEndDate: function getEndDate(date) {
  108. return date.endOf('month');
  109. },
  110. getWeekDay: function getWeekDay(date) {
  111. var clone = date.locale('en');
  112. return clone.weekday() + clone.localeData().firstDayOfWeek();
  113. },
  114. getYear: function getYear(date) {
  115. return date.year();
  116. },
  117. getMonth: function getMonth(date) {
  118. return date.month();
  119. },
  120. getDate: function getDate(date) {
  121. return date.date();
  122. },
  123. getHour: function getHour(date) {
  124. return date.hour();
  125. },
  126. getMinute: function getMinute(date) {
  127. return date.minute();
  128. },
  129. getSecond: function getSecond(date) {
  130. return date.second();
  131. },
  132. // set
  133. addYear: function addYear(date, diff) {
  134. return date.add(diff, 'year');
  135. },
  136. addMonth: function addMonth(date, diff) {
  137. return date.add(diff, 'month');
  138. },
  139. addDate: function addDate(date, diff) {
  140. return date.add(diff, 'day');
  141. },
  142. setYear: function setYear(date, year) {
  143. return date.year(year);
  144. },
  145. setMonth: function setMonth(date, month) {
  146. return date.month(month);
  147. },
  148. setDate: function setDate(date, num) {
  149. return date.date(num);
  150. },
  151. setHour: function setHour(date, hour) {
  152. return date.hour(hour);
  153. },
  154. setMinute: function setMinute(date, minute) {
  155. return date.minute(minute);
  156. },
  157. setSecond: function setSecond(date, second) {
  158. return date.second(second);
  159. },
  160. // Compare
  161. isAfter: function isAfter(date1, date2) {
  162. return date1.isAfter(date2);
  163. },
  164. isValidate: function isValidate(date) {
  165. return date.isValid();
  166. },
  167. locale: {
  168. getWeekFirstDay: function getWeekFirstDay(locale) {
  169. return dayjs().locale(parseLocale(locale)).localeData().firstDayOfWeek();
  170. },
  171. getWeekFirstDate: function getWeekFirstDate(locale, date) {
  172. return date.locale(parseLocale(locale)).weekday(0);
  173. },
  174. getWeek: function getWeek(locale, date) {
  175. return date.locale(parseLocale(locale)).week();
  176. },
  177. getShortWeekDays: function getShortWeekDays(locale) {
  178. return dayjs().locale(parseLocale(locale)).localeData().weekdaysMin();
  179. },
  180. getShortMonths: function getShortMonths(locale) {
  181. return dayjs().locale(parseLocale(locale)).localeData().monthsShort();
  182. },
  183. format: function format(locale, date, _format) {
  184. return date.locale(parseLocale(locale)).format(_format);
  185. },
  186. parse: function parse(locale, text, formats) {
  187. var localeStr = parseLocale(locale);
  188. for (var i = 0; i < formats.length; i += 1) {
  189. var format = formats[i];
  190. var formatText = text;
  191. if (format.includes('wo') || format.includes('Wo')) {
  192. // parse Wo
  193. var year = formatText.split('-')[0];
  194. var weekStr = formatText.split('-')[1];
  195. var firstWeek = dayjs(year, 'YYYY').startOf('year').locale(localeStr);
  196. for (var j = 0; j <= 52; j += 1) {
  197. var nextWeek = firstWeek.add(j, 'week');
  198. if (nextWeek.format('Wo') === weekStr) {
  199. return nextWeek;
  200. }
  201. }
  202. parseNoMatchNotice();
  203. return null;
  204. }
  205. var date = dayjs(formatText, format, true).locale(localeStr);
  206. if (date.isValid()) {
  207. return date;
  208. }
  209. }
  210. if (!text) {
  211. parseNoMatchNotice();
  212. }
  213. return null;
  214. }
  215. },
  216. toDate: function toDate(value, valueFormat) {
  217. if (Array.isArray(value)) {
  218. return value.map(function (val) {
  219. return typeof val === 'string' && val ? dayjs(val, valueFormat) : val || null;
  220. });
  221. } else {
  222. return typeof value === 'string' && value ? dayjs(value, valueFormat) : value || null;
  223. }
  224. },
  225. toString: function toString(value, valueFormat) {
  226. if (Array.isArray(value)) {
  227. return value.map(function (val) {
  228. return dayjs.isDayjs(val) ? val.format(valueFormat) : val;
  229. });
  230. } else {
  231. return dayjs.isDayjs(value) ? value.format(valueFormat) : value;
  232. }
  233. }
  234. };
  235. export default generateConfig;