Function to convert 2 digit hear to 4 digit

by Arvind Pal

JavaScript

function convertToFourDigitYear(year) {
  const currentYear = new Date().getFullYear(); // Get the current year
  const currentCentury = Math.floor(currentYear / 100) * 100; // Get the current century

  // Convert the year to a number and add the current century
  let fourDigitYear = Number(year) + currentCentury;

  // If the calculated 4-digit year is in the future, subtract 100 years
  if (fourDigitYear > currentYear) {
    fourDigitYear -= 100;
  }

  return fourDigitYear;
}

// Example usage
console.log(convertToFourDigitYear('99')); // Output: 1999
console.log(convertToFourDigitYear('20')); // Output: 2020

function compareQuarterAndYear(q1, y1, q2, y2) {
  // Convert quarter and year to numbers
  q1 = Number(q1);
  y1 = Number(y1);
  q2 = Number(q2);
  y2 = Number(y2);

  // Compare the years first
  if (y1 > y2) {
    return 1;
  } else if (y1 < y2) {
    return -1;
  }

  // If years are the same, compare the quarters
  if (q1 > q2) {
    return 1;
  } else if (q1 < q2) {
    return -1;
  } else {
    return 0;
  }
}

// Example usage
console.log(compareQuarterAndYear('1', '2021', '2', '2020')); // Output: 1
console.log(compareQuarterAndYear('3', '2022', '3', '2022')); // Output: 0
console.log(compareQuarterAndYear('4', '2019', '2', '2020')); // Output: -1