当前位置 : 主页 > 网络编程 > JavaScript >

获取当前时间(中文)

来源:互联网 收集:自由互联 发布时间:2021-06-28
使用getTime()就可以得到时间的字符串了 function getTime() {var dateStr = "";var date = new Date();dateStr += date.getFullYear() + "年";dateStr += date.getMonth() + 1 + "月";dateStr += date.getDate() + "日";dateStr += " ";dat
使用getTime()就可以得到时间的字符串了
function getTime() {
	var dateStr = "";
	var date = new Date();

	dateStr += date.getFullYear() + "年";
	dateStr += date.getMonth() + 1 + "月";
	dateStr += date.getDate() + "日";
	dateStr += " ";
	dateStr += "星期" + week(date.getDay());
	dateStr += " ";
	dateStr += doubleNum(toTwelve(date.getHours())) + ":";
	dateStr += doubleNum(date.getMinutes()) + ":";
	dateStr += doubleNum(date.getSeconds());
	dateStr += " ";
	dateStr += getroon(date.getHours());

	return dateStr;
}

function doubleNum(number) {
	if(number < 10) {
		return "0" + number;
	}
	return number;
}

function week(number) {
	switch(number) {
		case 0:
			return "日";
		case 1:
			return "一";
		case 2:
			return "二";
		case 3:
			return "三";
		case 4:
			return "四";
		case 5:
			return "五";
		case 6:
			return "六";
	}
}

function getroon(hour) {
	return hour < 13 ? "上午" : "下午";
}

function toTwelve(hour) {
	return hour < 13 ? hour : hour - 12;
}
上一篇:输出系统年月日
下一篇:省市js显示
网友评论