本文主要是介绍js 将日期转换成大写格式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近做了个需求将日期转换成大写汉字,网上搜的不对。进行改正后如下:
function dateToChinese(date) {var chinese = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];var y = date.getFullYear().toString();var m = date.getMonth().toString();var d = date.getDate().toString();var result = "";for (var i = 0; i < y.length; i++) {result += chinese[y.charAt(i)];}result += "年";if (m.length == 2) {if (m.charAt(0) == "1") {result += ("十" + chinese[m.charAt(1)] + "月");}} else {result += (chinese[m.charAt(0)] + "月");} if (d.length == 2) {result += (chinese[d.charAt(0)] + "十" + chinese[d.charAt(1)] + "日");} else {result += (chinese[d.charAt(0)] + "日");}return result;
}
取几个月后的日期(在原来基础上加几个月就行):
/**
* date 当前日期
* n 增加几个月(月数)
**/
function addNMonths(date, n) {var newDate = new Date(date);newDate.setMonth(newDate.getMonth() + n);return newDate;
}
这篇关于js 将日期转换成大写格式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!