Current Year Utility
The `year` package is a simple JavaScript utility designed to retrieve the current year, formatted as either two or four digits. Its primary function is to return `YYYY` by default or `YY` when explicitly requested. The current version, 0.2.1, was last updated in 2014, making it effectively abandoned and unmaintained. As such, there is no active release cadence, and it lacks support for modern JavaScript features like ESM. It differentiates itself through its extreme simplicity and minimal footprint, although modern alternatives offer more robust date formatting and are actively maintained. It targets Node.js environments version 0.8 and above, reflecting its age.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module ... from ... not supported.
cause Attempting to `require()` an ESM module, or more likely, trying to `import` this CJS module in an ESM context.fixThis package is CommonJS. If your project is ESM, either switch to `new Date().getFullYear()` directly, or convert your module to CommonJS. Do not use `import year from 'year';` -
TypeError: year is not a function
cause Likely due to incorrect import (e.g., `import { year } from 'year'` instead of `import year from 'year'`) or a failed CommonJS `require` where the module wasn't found.fixEnsure the CommonJS `const year = require('year');` syntax is used correctly. Verify the package is installed.
Warnings
- breaking This package is abandoned and has not been updated since 2014. It relies on an internal date mechanism that might be outdated or behave unexpectedly in modern JavaScript runtimes or with time zone changes. It is strongly recommended to use `new Date().getFullYear()` for reliability.
- gotcha The package is CommonJS-only. Attempting to use `import` syntax in a pure ESM module will lead to an `ERR_REQUIRE_ESM` error.
- deprecated The package is unmaintained, meaning it will not receive security updates, bug fixes, or compatibility improvements for newer Node.js versions or browser environments. Using it may introduce security vulnerabilities or unexpected behavior.
Install
-
npm install year -
yarn add year -
pnpm add year
Imports
- year
import year from 'year';
const year = require('year'); - year()
const currentYear = year();
- year('yy')
const twoDigitYear = year('yy');
Quickstart
const year = require('year');
// Get the current year (e.g., 2014 when this was published)
const fullYear = year();
console.log(`Current full year: ${fullYear}`);
// Get the current year in 4-digit format (explicitly)
const explicitFullYear = year('yyyy');
console.log(`Explicit full year: ${explicitFullYear}`);
// Get the current year in 2-digit format
const twoDigitYear = year('yy');
console.log(`Two-digit year: ${twoDigitYear}`);
// Example using current date to demonstrate expected output (since 'year' is fixed to 2014 in examples)
const actualCurrentYear = new Date().getFullYear();
console.log(`Actual current year (YYYY): ${actualCurrentYear}`);
console.log(`Actual current year (YY): ${String(actualCurrentYear).slice(-2)}`);