year.js (1827B)
1import { makeGetSet } from '../moment/get-set'; 2import { addFormatToken } from '../format/format'; 3import { 4 addRegexToken, 5 match1to2, 6 match1to4, 7 match1to6, 8 match2, 9 match4, 10 match6, 11 matchSigned, 12} from '../parse/regex'; 13import { addParseToken } from '../parse/token'; 14import { isLeapYear } from '../utils/is-leap-year'; 15import { hooks } from '../utils/hooks'; 16import { YEAR } from './constants'; 17import toInt from '../utils/to-int'; 18import zeroFill from '../utils/zero-fill'; 19 20// FORMATTING 21 22addFormatToken('Y', 0, 0, function () { 23 var y = this.year(); 24 return y <= 9999 ? zeroFill(y, 4) : '+' + y; 25}); 26 27addFormatToken(0, ['YY', 2], 0, function () { 28 return this.year() % 100; 29}); 30 31addFormatToken(0, ['YYYY', 4], 0, 'year'); 32addFormatToken(0, ['YYYYY', 5], 0, 'year'); 33addFormatToken(0, ['YYYYYY', 6, true], 0, 'year'); 34 35// PARSING 36 37addRegexToken('Y', matchSigned); 38addRegexToken('YY', match1to2, match2); 39addRegexToken('YYYY', match1to4, match4); 40addRegexToken('YYYYY', match1to6, match6); 41addRegexToken('YYYYYY', match1to6, match6); 42 43addParseToken(['YYYYY', 'YYYYYY'], YEAR); 44addParseToken('YYYY', function (input, array) { 45 array[YEAR] = 46 input.length === 2 ? hooks.parseTwoDigitYear(input) : toInt(input); 47}); 48addParseToken('YY', function (input, array) { 49 array[YEAR] = hooks.parseTwoDigitYear(input); 50}); 51addParseToken('Y', function (input, array) { 52 array[YEAR] = parseInt(input, 10); 53}); 54 55// HELPERS 56 57export function daysInYear(year) { 58 return isLeapYear(year) ? 366 : 365; 59} 60 61export { isLeapYear }; 62 63// HOOKS 64 65hooks.parseTwoDigitYear = function (input) { 66 return toInt(input) + (toInt(input) > 68 ? 1900 : 2000); 67}; 68 69// MOMENTS 70 71export var getSetYear = makeGetSet('FullYear', true); 72 73export function getIsLeapYear() { 74 return isLeapYear(this.year()); 75}