JavaScript日期Format格式化

1、Date类拓展方法

Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份 
"d+": this.getDate(), //日 
"H+": this.getHours(), //小时 
"m+": this.getMinutes(), //分 
"s+": this.getSeconds(), //秒 
"q+": Math.floor((this.getMonth() + 3) / 3), //季度 
"S": this.getMilliseconds() //毫秒 
};

var week = {           
"0" : "u65e5",           
"1" : "u4e00",           
"2" : "u4e8c",           
"3" : "u4e09",           
"4" : "u56db",           
"5" : "u4e94",           
"6" : "u516d"          
    }; 

if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}

if(/(E+)/.test(fmt)){           
        fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "u661fu671f" : "u5468") : "")+week[this.getDay()+""]);           
    }
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

return fmt;
}

2、格式化格式

  • 月(M)、日(d)、小时(H)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符
  • 年(y)可以用 1-4 个占位符
  • 毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  • 周(E),可以用1-3个占位符

3、格式化方法

1)格式化时间戳

function formatDate(timestamp, format) {
return new Date(parseInt(timestamp)).Format(format);
}

2)格式化短日期

function formatTDate(time) {
return formatDate(time, "yyyy年MM月dd日");
}

3)格式化年份

function formatTDYear(time) {
return formatDate(time, "yyyy");
}

function formatTDYearCN(time) {
return formatCNYear(formatDate(time, "yyyy"));
}

function formatCNYear(num) {
var map = {"0":"零", "1":"一", "2":"二", "3":"三", "4":"四", "5":"五", "6":"六", "7":"七", "8":"八", "9":"九"};
var result = "";
var temp = parseInt(num);

while (temp > 0){
result = map[temp % 10] + result;
temp = parseInt(temp / 10);
}

return result;
}

4)格式化月份

function formatTDMonth(time) {
return formatDate(time, "M");
}

function formatTDMonthEN(time) {
var month = formatDate(time, "M");
var map = {"1":"Jan", "2":"Feb", "3":"Mar", "4":"Apr", "5":"May", "6":"Jun", "7":"Jul", "8":"Aug", "9":"Sept", "10":"Oct", "11":"Nov", "12":"Dec"};
return map[month];
}

function formatTDMonthENL(time) {
var month = formatDate(time, "M");
var map = {"1":"January", "2":"February", "3":"March", "4":"April", "5":"May", "6":"June", "7":"July", "8":"August", "9":"September", "10":"October", "11":"November", "12":"December"};
return map[month];
}

function formatTDMonthCN(time) {
return formatCNDateNumber(formatDate(time, "M"));
}

function formatTDMonthCNL(time) {
return formatCNDateNumber(formatDate(time, "M")) + "月";
}

function formatCNDateNumber(num) {
var map = {"0":"零", "1":"一", "2":"二", "3":"三", "4":"四", "5":"五", "6":"六", "7":"七", "8":"八", "9":"九"};
var result = "";

if (num >= 10) {
result += map[parseInt(num / 10)];
result += "十";

if (num % 10 > 0) {
result += map[num % 10];
}
} else {
result += map[num % 10];
}

return result;
}

5)格式化天

function formatTDDay(time) {
return formatDate(time, "d");
}

function formatTDDayCN(time) {
return formatCNDateNumber(formatDate(time, "d"));
}

6)格式化周

function formatTDWeekEN(time) {
var week = formatDate(time, "EE");
var map = {"周日":"Sun", "周一":"Mon", "周二":"Tue", "周三":"Wed", "周四":"Thu", "周五":"Fri", "周六":"Sat"};
return map[week];
}

function formatTDWeekENL(time) {
var week = formatDate(time, "EE");
var map = {"周日":"Sunday", "周一":"Monday", "周二":"Tuesday", "周三":"Wednesday", "周四":"Thursday", "周五":"Friday", "周六":"Saturday"};
return map[week];
}

function formatTDWeekCN(time) {
return formatDate(time, "EE");
}

function formatTDWeekCNL(time) {
return formatDate(time, "EEE");
}

7)格式化时间

function formatTTime(time) {
return formatDate(time, "HH:mm");
}

function formatTTHour(time) {
return formatDate(time, "H");
}

function formatTTHourCN(time) {
return formatCNDateNumber(formatDate(time, "H"));
}

function formatTTMinite(time) {
return formatDate(time, "m");
}

function formatTTMiniteCN(time) {
return formatCNDateNumber(formatDate(time, "m"));
}

8)格式化时间段

function formatTime(startTime, endTime) {
var timeStr = "";

if (endTime != undefined) {
if (formatDate(startTime, "yyyy-MM-dd") == formatDate(endTime, "yyyy-MM-dd")) {
timeStr += formatDate(startTime, "yyyy-MM-dd");
timeStr += " ";
timeStr += formatDate(startTime, "HH:mm");
timeStr += "-";
timeStr += formatDate(endTime, "HH:mm");
} else {
timeStr += formatDate(startTime, "yyyy-MM-dd HH:mm");
timeStr += " 至 ";
timeStr += formatDate(endTime, "yyyy-MM-dd HH:mm");
}
} else {
timeStr += formatDate(startTime, "yyyy-MM-dd HH:mm");
}

return timeStr;
}

发表评论