JS format switching between 12-hour time and 24-hour time

Determine whether it is a 12-hour format or a 24-hour format? ? ?

function getTimeFormat(time) {
    
    
  const regex24Hour = /^([01]\d|2[0-3]):?([0-5]\d)$/;
  const regex12Hour = /^(0?[1-9]|1[0-2]):?([0-5]\d)\s?(AM|PM)$/i;

  if (regex24Hour.test(time)) {
    
    
    return '24-hour format';
  } else if (regex12Hour.test(time)) {
    
    
    return '12-hour format';
  } else {
    
    
    return 'Unknown format';
  }
}

const time1 = '14:00';
const time2 = '2:00 PM';

console.log(getTimeFormat(time1));  // 输出:24-hour format
console.log(getTimeFormat(time2));  // 输出:12-hour format

24 to 12

function convertTo12HourFormat(time) {
    
    
  const [hour, minute] = time.split(':');
  let convertedHour = parseInt(hour, 10);
  let suffix = '';

  if (convertedHour >= 12) {
    
    
    suffix = 'PM';
    if (convertedHour > 12) {
    
    
      convertedHour -= 12;
    }
  } else {
    
    
    suffix = 'AM';
    if (convertedHour === 0) {
    
    
      convertedHour = 12;
    }
  }

  const formattedHour = convertedHour.toString().padStart(2, '0');
  const formattedMinute = minute.toString().padStart(2, '0');

  return `${
      
      formattedHour}:${
      
      formattedMinute} ${
      
      suffix}`;
}

const time24Hour = '14:00';
const time12Hour = convertTo12HourFormat(time24Hour);
console.log(time12Hour);  // 输出:2:00 PM

12 to 24

function convertTo24HourFormat(time) {
    
    
  const [hour, minute, suffix] = time.split(/:|\s/);
  let convertedHour = parseInt(hour, 10);

  if (suffix === 'PM' && convertedHour !== 12) {
    
    
    convertedHour += 12;
  } else if (suffix === 'AM' && convertedHour === 12) {
    
    
    convertedHour = 0;
  }

  const formattedHour = convertedHour.toString().padStart(2, '0');
  const formattedMinute = minute.toString().padStart(2, '0');

  return `${
      
      formattedHour}:${
      
      formattedMinute}`;
}

const time12Hour = '2:00 PM';
const time24Hour = convertTo24HourFormat(time12Hour);
console.log(time24Hour);  // 输出:14:00

Supplementary knowledge points:
.padStart() is a built-in method of JavaScript strings, which is used to pad the specified characters at the beginning of the current string until the string reaches the specified length. This method can be used to complete strings to ensure that the length of the string meets certain requirements.

The .padStart( targetLength , padString ) method accepts two parameters:

targetLength : The minimum length to be reached, that is, the length of the desired string after filling.
padString (optional): The character to use for padding, which is repeated until the padded string reaches the specified length. If this parameter is not provided, a space character is used as padding character by default.
Here are some examples to demonstrate the use of the .padStart() method:

const str1 = 'hello';
console.log(str1.padStart(10));  // 输出:'     hello',用空格填充到长度为 10

const str2 = 'hello';
console.log(str2.padStart(10, '-'));  // 输出:'-----hello',用连字符填充到长度为 10

const str3 = 'hello';
console.log(str3.padStart(3));  // 输出:'hello',不进行填充,因为字符串已经达到或超过指定的长度

const str4 = '42';
console.log(str4.padStart(6, '0'));  // 输出:'000042',用零填充到长度为 6

Similarly, the .padEnd method is called to pad the specified characters at the end of the string until the length of the string reaches or exceeds the target length. This ensures that the string has the minimum required length, padded with the specified characters.

const str1 = 'hello';
console.log(str1.padEnd(10));  // 输出:'hello     ',用空格填充到长度为 10

const str2 = 'hello';
console.log(str2.padEnd(10, '-'));  // 输出:'hello-----',用连字符填充到长度为 10

const str3 = 'hello';
console.log(str3.padEnd(3));  // 输出:'hello',不进行填充,因为字符串已经达到或超过指定的长度

const str4 = '42';
console.log(str4.padEnd(6, '0'));  // 输出:'420000',用零填充到长度为 6

Guess you like

Origin blog.csdn.net/z2000ky/article/details/130986463