time格式加入中文說明文字

by cactus77kiki

JavaScript

/*http://www.voidcn.com/article/p-wwfxiqnv-btt.html*/
function tConvert (time) {
	var tran ="";
  // Check correct time format and split into components
  time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];

  if (time.length > 1) { // If time format correct
    time = time.slice (1);  // Remove full string match value    
    tran = time[0] < 12 ? '上午' : '下午'; // Set AM/PM    
    //time[5] = +time[0] < 12 ? '上午' : '下午'; // Set AM/PM
    //time[5] = +time[0] < 12 ? 'AM' : 'PM'; // Set AM/PM
    time[0] = +time[0] % 12 || 12; // Adjust hours
  }
  return tran+time.join (''); // return adjusted time or original string
}

var test =tConvert ('18:00:00');
console.log(test);