console.clear();
const now = new Date();
const dateOptions = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
};
const dateOptions2 = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
};
const dateOptions3 = {
month: 'long',
day: 'numeric'
};
const timeOptions = {
hour12: false,
hour: '2-digit',
minute: '2-digit',
};
console.log( '目前時間是 ' + now.toLocaleString('zh-TW', {dateOptions, timeOptions}) );
console.log( now.toLocaleString('zh-TW', dateOptions2) );
console.log( now.toLocaleString('zh-TW', dateOptions2).replace(/\//g, '-'));
console.log( '今天是' + now.toLocaleString('zh-TW', { weekday: 'long' }) );
const timeStr = now.toLocaleString('zh-TW', timeOptions);
console.log(`今天是${now.toLocaleString('zh-TW', dateOptions3)},時間為 ${timeStr}`);
console.log(`今天是${toC(now.getMonth() + 1)}月${toC(now.getDate())}日,時間為 ${timeStr}`);
function toC(s) {
const upperCaseNumber = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
let n = Number(s);
if (n < 10) {
return upperCaseNumber[n];
} else {
let str = '';
Math.floor(n / 10) === 1 ? str += '十' : str += upperCaseNumber[Math.floor(n / 10)] + '十';
if (n % 10 === 0) {
return str;
}
return str + upperCaseNumber[n % 10];
}
}
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.