获取本地时间和北京时间
sj.html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link type="text/css" rel="stylesheet" href="css/index.css" />
</head>
<script>
function add_zero(temp) {
if (temp < 10) return "0" + temp;
else return temp;
}
function t() {
var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
xmlhttp.open("GET", "date.aspx", false);
xmlhttp.setRequestHeader("If-Modified-Since", "bjtime");
xmlhttp.send();
var dateStr = xmlhttp.getResponseHeader("Date");
document.getElementById("bjsj").innerHTML = xmlhttp.responseText;
var date = new Date(dateStr);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var date1 = date.getDate();
var hour = date.getHours();
var minutes = date.getMinutes();
var second = date.getSeconds();
var apm = "AM";
if (hour > 12) {
apm = "PM";
hour = hour - 12;
}
var week1;
switch (date.getDay()) {
case 1: week1 = "Monday"; break;
case 2: week1 = "Tuesday"; break;
case 3: week1 = "Wednesday"; break;
case 4: week1 = "Thursday "; break;
case 5: week1 = "Friday"; break;
case 6: week1 = "Saturday "; break;
default: week1 = "Sunday";
}
var d = new Date();
var week;
switch (d.getDay()) {
case 1: week = "Monday"; break;
case 2: week = "Tuesday"; break;
case 3: week = "Wednesday"; break;
case 4: week = "Thursday "; break;
case 5: week = "Friday"; break;
case 6: week = "Saturday "; break;
default: week = "Sunday";
}
var years1 = d.getYear();
var month1 = d.getMonth() + 1;
var days1 = d.getDate();
var hour1 = d.getHours();
var minutes1 = d.getMinutes();
var second1 = d.getSeconds();
var apm1 = "AM";
if (hour1 > 12) {
apm1 = "PM";
hour1 = hour1 - 12;
}
//document.getElementById("bjsj").innerHTML = add_zero(hour) + ":" + add_zero(minutes) + " " + apm + " " + add_zero(month) + "/" + add_zero(date1) + "/" + year + " " + week1;
document.getElementById("ddsj").innerHTML = add_zero(hour1) + ":" + add_zero(minutes1) + " " + apm1 + " " + add_zero(month1) + "/" + add_zero(days1) + "/" + years1 + " " + week;
var mytime = setTimeout("t()", 60000);
}
</script>
<body onload="t()">
<div class="right sj">
Beijing time: <span id="bjsj"></span>Local time: <span id="ddsj"></span></div>
</body>
</html>
date.aspx页面的代码
protected void Page_Load(object sender, EventArgs e)
{
DateTime d = DateTime.Now;
int year = d.Year;
string month = add_zero(d.Month);
string day = add_zero(d.Day);
string week = d.DayOfWeek.ToString();
if (Request.QueryString["cn"] != "" && Request.QueryString["cn"] != null)
{
switch (week)
{
case "Monday": week = "星期一"; break;
case "Tuesday": week = "星期二"; break;
case "Wednesday": week = "星期三"; break;
case "Thursday": week = "星期四"; break;
case "Friday": week = "星期五"; break;
case "Saturday": week = "星期六 "; break;
default: week = "星期日"; break;
}
}
int hour = d.Hour;
string apm = "AM";
if (hour > 12)
{
apm = "PM";
hour = hour - 12;
}
string hours = add_zero(hour);
string minute = add_zero(d.Minute);
Response.Write(hours + ":" + minute + " " + apm + " " + month + "/" + day + "/" + year + " " + week);
}
protected string add_zero(int temp) {
string t="";
if (temp < 10)
{
t = "0" + temp.ToString();
}
else
{
t = temp.ToString();
}
return t;
}
引用部分
<iframe src="sj.html" frameborder="0" scrolling="no" height="50px"></iframe>