Source

dateTime/toFullYear.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isStr_1 = __importDefault(require("../string/isStr"));
var zeroPadding_1 = __importDefault(require("../number/zeroPadding"));
var CENTURY_DIGITS = 2;
/**
 * Convert 2 digit year to 4 digit year
 * @param year
 * @category dateTime
 * @module toFullYear
 */
var toFullYear = function (year) {
    var yearString = (0, isStr_1.default)(year) ? year : (0, zeroPadding_1.default)(year, CENTURY_DIGITS);
    var currentYear = new Date().getFullYear();
    // Index of current / prev / next decade
    var currentCentury = "".concat(currentYear).slice(0, CENTURY_DIGITS);
    var prevCentury = parseInt(currentCentury) - 1;
    var nextCentury = parseInt(currentCentury) + 1;
    // Get full years of the year if it is in current / prev / next decade
    var currentCenturyYear = parseInt("".concat(currentCentury).concat(yearString));
    var prevCenturyYear = parseInt("".concat(prevCentury).concat(yearString));
    var nextCenturyYear = parseInt("".concat(nextCentury).concat(yearString));
    // Find the one closest to currentYear
    var minDiff = Infinity;
    var fullYear = [
        currentCenturyYear,
        prevCenturyYear,
        nextCenturyYear,
    ].reduce(function (fullYear, year) {
        var diff = Math.abs(year - currentYear);
        if (diff < minDiff) {
            minDiff = diff;
            return year;
        }
        return fullYear;
    }, undefined);
    return fullYear;
};
exports.default = toFullYear;