Get the correct century from 2-digit year date value - JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

For this, you can use ternary operator based on some condition.

Example

Following is the code −

const yearRangeValue = 18;
const getCorrectCentury = dateValues => {
   var [date, month, year] = dateValues.split("-");
   var originalYear = +year > yearRangeValue ? "20" + year : "18" + year;
   return new Date(date + "-" + month + "-" + originalYear).toLocaleDateString('en-GB')
}; 
console.log(getCorrectCentury('10-JAN-19'));

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo300.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo300.js
1/10/2019
raja
Published on 09-Nov-2020 15:40:40
Advertisements