Last active
April 25, 2017 01:50
-
-
Save axetroy/bd2950774f3501d2ba0219295a95a9b1 to your computer and use it in GitHub Desktop.
将日期转化为中文可读性的日期,如:二〇一五年三月十二日 星期三 五时三十八分
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function date2ChineseHumanize(date) { | |
let DATE_CN = ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九']; | |
let DATE_UNIT = ['年', '月', '日', '星期', '时', '分', '秒']; | |
let dateArr = [ | |
date.getFullYear(), // 年 | |
date.getMonth() + 1, // 月 | |
date.getDate(), // 日 | |
date.getDay(), // 星期 | |
date.getHours(), // 小时 | |
date.getMinutes(), // 分钟 | |
date.getSeconds() // 秒 | |
]; | |
return dateArr | |
.map((time, index) => { | |
time = time + ''; | |
// 首先先全部替换成为汉字 | |
let result = time.split('').map(word => DATE_CN[+word]).join(''); | |
// 除了年份以外,其他要进行这样的转换: 12 >= 十二 | |
if (index !== 0 && +time > 10) { | |
let mod = +time % 10; | |
// 处理整数, 10 >= 十, 20 >= 二十 | |
if (mod === 0) { | |
result = +time === 10 ? '十' : result[0] + '十'; | |
} | |
else if(+time<20){ | |
result = '十' + result[1]; | |
} | |
else { | |
result = result[0] + '十' + result[1]; | |
} | |
} | |
// 处理星期天 | |
if (index === 3 && +time === 0) { | |
result = '天'; | |
} | |
// 最后处理星期,应该这样显示: 星期x,其他的显示xx年, xx月 | |
return index === 3 | |
? ' ' + DATE_UNIT[index] + result + ' ' | |
: result + DATE_UNIT[index]; | |
}) | |
.join(''); | |
} |
Author
axetroy
commented
Apr 24, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment