dayjs.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. dayjs.extend(customParseFormat);
  9. dayjs.extend(advancedFormat);
  10. dayjs.extend(weekday);
  11. dayjs.extend(localeData);
  12. dayjs.extend(weekOfYear);
  13. dayjs.extend(weekYear);
  14. dayjs.extend(function (o, c) {
  15. // todo support Wo (ISO week)
  16. var proto = c.prototype;
  17. var oldFormat = proto.format;
  18. proto.format = function f(formatStr) {
  19. var str = (formatStr || '').replace('Wo', 'wo');
  20. return oldFormat.bind(this)(str);
  21. };
  22. });
  23. var localeMap = {
  24. // ar_EG:
  25. // az_AZ:
  26. // bg_BG:
  27. bn_BD: 'bn-bd',
  28. by_BY: 'be',
  29. // ca_ES:
  30. // cs_CZ:
  31. // da_DK:
  32. // de_DE:
  33. // el_GR:
  34. en_GB: 'en-gb',
  35. en_US: 'en',
  36. // es_ES:
  37. // et_EE:
  38. // fa_IR:
  39. // fi_FI:
  40. fr_BE: 'fr',
  41. // todo: dayjs has no fr_BE locale, use fr at present
  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. /* istanbul ignore next */
  96. var parseNoMatchNotice = function parseNoMatchNotice() {
  97. // zombieJ:
  98. // When user typing, its always miss match format.
  99. // This check is meaningless.
  100. // https://github.com/ant-design/ant-design/issues/51839
  101. // noteOnce(false, 'Not match any format. Please help to fire a issue about this.');
  102. };
  103. var generateConfig = {
  104. // get
  105. getNow: function getNow() {
  106. var now = dayjs();
  107. // https://github.com/ant-design/ant-design/discussions/50934
  108. if (typeof now.tz === 'function') {
  109. return now.tz(); // use default timezone
  110. }
  111. return now;
  112. },
  113. getFixedDate: function getFixedDate(string) {
  114. return dayjs(string, ['YYYY-M-DD', 'YYYY-MM-DD']);
  115. },
  116. getEndDate: function getEndDate(date) {
  117. return date.endOf('month');
  118. },
  119. getWeekDay: function getWeekDay(date) {
  120. var clone = date.locale('en');
  121. return clone.weekday() + clone.localeData().firstDayOfWeek();
  122. },
  123. getYear: function getYear(date) {
  124. return date.year();
  125. },
  126. getMonth: function getMonth(date) {
  127. return date.month();
  128. },
  129. getDate: function getDate(date) {
  130. return date.date();
  131. },
  132. getHour: function getHour(date) {
  133. return date.hour();
  134. },
  135. getMinute: function getMinute(date) {
  136. return date.minute();
  137. },
  138. getSecond: function getSecond(date) {
  139. return date.second();
  140. },
  141. getMillisecond: function getMillisecond(date) {
  142. return date.millisecond();
  143. },
  144. // set
  145. addYear: function addYear(date, diff) {
  146. return date.add(diff, 'year');
  147. },
  148. addMonth: function addMonth(date, diff) {
  149. return date.add(diff, 'month');
  150. },
  151. addDate: function addDate(date, diff) {
  152. return date.add(diff, 'day');
  153. },
  154. setYear: function setYear(date, year) {
  155. return date.year(year);
  156. },
  157. setMonth: function setMonth(date, month) {
  158. return date.month(month);
  159. },
  160. setDate: function setDate(date, num) {
  161. return date.date(num);
  162. },
  163. setHour: function setHour(date, hour) {
  164. return date.hour(hour);
  165. },
  166. setMinute: function setMinute(date, minute) {
  167. return date.minute(minute);
  168. },
  169. setSecond: function setSecond(date, second) {
  170. return date.second(second);
  171. },
  172. setMillisecond: function setMillisecond(date, milliseconds) {
  173. return date.millisecond(milliseconds);
  174. },
  175. // Compare
  176. isAfter: function isAfter(date1, date2) {
  177. return date1.isAfter(date2);
  178. },
  179. isValidate: function isValidate(date) {
  180. return date.isValid();
  181. },
  182. locale: {
  183. getWeekFirstDay: function getWeekFirstDay(locale) {
  184. return dayjs().locale(parseLocale(locale)).localeData().firstDayOfWeek();
  185. },
  186. getWeekFirstDate: function getWeekFirstDate(locale, date) {
  187. return date.locale(parseLocale(locale)).weekday(0);
  188. },
  189. getWeek: function getWeek(locale, date) {
  190. return date.locale(parseLocale(locale)).week();
  191. },
  192. getShortWeekDays: function getShortWeekDays(locale) {
  193. return dayjs().locale(parseLocale(locale)).localeData().weekdaysMin();
  194. },
  195. getShortMonths: function getShortMonths(locale) {
  196. return dayjs().locale(parseLocale(locale)).localeData().monthsShort();
  197. },
  198. format: function format(locale, date, _format) {
  199. return date.locale(parseLocale(locale)).format(_format);
  200. },
  201. parse: function parse(locale, text, formats) {
  202. var localeStr = parseLocale(locale);
  203. for (var i = 0; i < formats.length; i += 1) {
  204. var format = formats[i];
  205. var formatText = text;
  206. if (format.includes('wo') || format.includes('Wo')) {
  207. // parse Wo
  208. var year = formatText.split('-')[0];
  209. var weekStr = formatText.split('-')[1];
  210. var firstWeek = dayjs(year, 'YYYY').startOf('year').locale(localeStr);
  211. for (var j = 0; j <= 52; j += 1) {
  212. var nextWeek = firstWeek.add(j, 'week');
  213. if (nextWeek.format('Wo') === weekStr) {
  214. return nextWeek;
  215. }
  216. }
  217. parseNoMatchNotice();
  218. return null;
  219. }
  220. var date = dayjs(formatText, format, true).locale(localeStr);
  221. if (date.isValid()) {
  222. return date;
  223. }
  224. }
  225. if (text) {
  226. parseNoMatchNotice();
  227. }
  228. return null;
  229. }
  230. }
  231. };
  232. export default generateConfig;