8c1562f6b3644cba7f26ef133d1e9b0646ee80d215e022159f49534c77b234bc1527390ab7628436aee0c6d7c1c3e1292ce03129a39debcf1d0eebc1729ced 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { makeGetSet } from '../moment/get-set';
  2. import { addFormatToken } from '../format/format';
  3. import { addUnitAlias } from './aliases';
  4. import { addRegexToken, match1to2, match1to4, match1to6, match2, match4, match6, matchSigned } from '../parse/regex';
  5. import { addParseToken } from '../parse/token';
  6. import { hooks } from '../utils/hooks';
  7. import { YEAR } from './constants';
  8. import toInt from '../utils/to-int';
  9. // FORMATTING
  10. addFormatToken('Y', 0, 0, function () {
  11. var y = this.year();
  12. return y <= 9999 ? '' + y : '+' + y;
  13. });
  14. addFormatToken(0, ['YY', 2], 0, function () {
  15. return this.year() % 100;
  16. });
  17. addFormatToken(0, ['YYYY', 4], 0, 'year');
  18. addFormatToken(0, ['YYYYY', 5], 0, 'year');
  19. addFormatToken(0, ['YYYYYY', 6, true], 0, 'year');
  20. // ALIASES
  21. addUnitAlias('year', 'y');
  22. // PARSING
  23. addRegexToken('Y', matchSigned);
  24. addRegexToken('YY', match1to2, match2);
  25. addRegexToken('YYYY', match1to4, match4);
  26. addRegexToken('YYYYY', match1to6, match6);
  27. addRegexToken('YYYYYY', match1to6, match6);
  28. addParseToken(['YYYYY', 'YYYYYY'], YEAR);
  29. addParseToken('YYYY', function (input, array) {
  30. array[YEAR] = input.length === 2 ? hooks.parseTwoDigitYear(input) : toInt(input);
  31. });
  32. addParseToken('YY', function (input, array) {
  33. array[YEAR] = hooks.parseTwoDigitYear(input);
  34. });
  35. addParseToken('Y', function (input, array) {
  36. array[YEAR] = parseInt(input, 10);
  37. });
  38. // HELPERS
  39. export function daysInYear(year) {
  40. return isLeapYear(year) ? 366 : 365;
  41. }
  42. function isLeapYear(year) {
  43. return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
  44. }
  45. // HOOKS
  46. hooks.parseTwoDigitYear = function (input) {
  47. return toInt(input) + (toInt(input) > 68 ? 1900 : 2000);
  48. };
  49. // MOMENTS
  50. export var getSetYear = makeGetSet('FullYear', true);
  51. export function getIsLeapYear () {
  52. return isLeapYear(this.year());
  53. }