$("LabelWelcome").innerHTML = GetWelcomeByTime() + " 今天是:" + GetSimpleDate() + " " + GetWeekDay() + " " + GetCnDate() + SolarTerm(new Date());
}
function GetWelcomeByTime() {
var now = new Date();
var hour = now.getHours();
var hello = "";
if (hour < 4) {
hello = "夜深了";
}
else if (hour < 7) {
hello = "早安";
}
else if (hour < 9) {
hello = "早上好";
}
else if (hour < 12) {
hello = "上午好";
}
else if (hour < 14) {
hello = "中午好";
}
else if (hour < 17) {
hello = "下午好";
}
else if (hour < 19) {
hello = "您好";
}
else if (hour < 22) {
hello = "晚上好";
}
else {
hello = "夜深了";
}
return hello;
}
function GetSimpleDate() {
var today = new Date();
var date = (today.getYear() < 100 ? today.getYear() + 1900 : today.getYear()) + "年" + (today.getMonth() + 1) + "月" + today.getDate() + "日";
return date;
}
function GetWeekDay() {
var today = new Date();
var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
return weeks[today.getDay()];
}
//获取农历日期
function GetCnDate() {
var today = new Date();
return CnDateofDateStr(today);
}
function CnDateofDateStr(DateGL) {
if (CnMonthofDate(DateGL) == "零月") return " 请调整您的计算机日期!";
else return "农历:" + CnYearofDate(DateGL) + " " + CnMonthofDate(DateGL) + CnDayofDate(DateGL);
}
function SolarTerm(DateGL) {
var SolarTermStr = new Array(
"小寒", "大寒", "立春", "雨水", "惊蛰", "春分",
"清明", "谷雨", "立夏", "小满", "芒种", "夏至",
"小暑", "大暑", "立秋", "处暑", "白露", "秋分",
"寒露", "霜降", "立冬", "小雪", "大雪", "冬至");
var DifferenceInMonth = new Array(
1272060, 1275495, 1281180, 1289445, 1299225, 1310355,
1321560, 1333035, 1342770, 1350855, 1356420, 1359045,
1358580, 1355055, 1348695, 1340040, 1329630, 1318455,
1306935, 1297380, 1286865, 1277730, 1274550, 1271556);
var DifferenceInYear = 31556926;
var BeginTime = new Date(1901 / 1 / 1);
BeginTime.setTime(947120460000);
for (; DateGL.getYear() < BeginTime.getYear(); ) {
BeginTime.setTime(BeginTime.getTime() - DifferenceInYear * 1000);
}
for (; DateGL.getYear() > BeginTime.getYear(); ) {
BeginTime.setTime(BeginTime.getTime() + DifferenceInYear * 1000);
}
for (var M = 0; DateGL.getMonth() > BeginTime.getMonth(); M++) {
BeginTime.setTime(BeginTime.getTime() + DifferenceInMonth[M] * 1000);
}
if (DateGL.getDate() > BeginTime.getDate()) {
BeginTime.setTime(BeginTime.getTime() + DifferenceInMonth[M] * 1000);
M++;
}
if (DateGL.getDate() > BeginTime.getDate()) {
BeginTime.setTime(BeginTime.getTime() + DifferenceInMonth[M] * 1000);
M == 23 ? M = 0 : M++;
}
var JQ;
if (DateGL.getDate() == BeginTime.getDate()) {
JQ = " 今天是<font color='#FF9999'><b>" + SolarTermStr[M] + "</b></font>";
}
else {
JQ = " "
}
return JQ;
}
Javascript无刷新获取当前时间 在项目中需要无刷新更新时间,并且TooTip中显示年月日星期,网上搜集了很多方法,总结一下,以备下次之需。
view sourceprint?<script language="javascript" type="text/javascript">
function ShowTime() {
document.getElementById("time").innerHTML = new Date().toLocaleTimeString(); //获取当前系统时间
setTimeout("ShowTime()", 1000);//设置定时刷新 一秒更新一次
}
function DateDemo() {
var d, day = ""; //声明变量
d = new Date(); //创建Date对象