- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the correct century from 2-digit year date value - JavaScript?
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
Advertisements