C#获取服务器时间??急急急请高手帮忙..

C#获取服务器时间??急急急请高手帮忙..,第1张

唉,花了点时间,现做了一个,你把下面的代码保存为ASPX就可以运行了,我已经测试通过

<form runat=server>

下面这个Hidden变量用来暂存服务器时间

<input name="ServerTime" type="hidden" value="<%=DateTimeNowToString("yyyy-MM-dd HH:mm:ss")%>">

<input name="divCurrentUser" type="text" style="width:300px;border:0" readonly>

</form>

<script language=javascript>

getServerTime();

//计算当前服务器时间

function getServerTime()

{

//取得要进行显示的日期

var datetimeYou = documentgetElementById('ServerTime')valuesplit(" ");

var yout1 = datetimeYou[0]split("-");

var yout2 = datetimeYou[1]split(":");

var nowY = parseFloat(yout1[0]);

var nowM = parseFloat(yout1[1]);

var nowD = parseFloat(yout1[2]);

var h = parseFloat(yout2[0]);

var m = parseFloat(yout2[1]);

var s = parseFloat(yout2[2]);

var daysMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

if(leapyear(nowY))

daysMonth[1] += 1;//闰年,2月加1

s = s + 1;

if (s >= 60)

{

s = 0;

m = m + 1;

if (m >= 60)

{

m = 0;

h = h + 1;

if (h >= 24)

{

h = 0;

nowD += 1;

if(nowD > daysMonth[nowM-1])

{

nowD = 1;

nowM += 1;

if(nowM > 12)

{

nowM = 1;

nowY += 1;

}

}

}

}

}

if (nowM < 10) nowM = "0" + nowM;

if (nowD < 10) nowD = "0" + nowD;

if (h < 10) h = "0" + h;

if (m < 10) m = "0" + m;

if (s < 10) s = "0" + s;

documentgetElementById('ServerTime')value = nowY + "-" + nowM + "-" + nowD + " " + h + ":" + m + ":" + s;

var today,hour,second,minute,year,month,date;

var strDate ;

//注意:Javascript中的月要减1,比如现在4月,要写成3

today=new Date(yout1[0],yout1[1]-1,yout1[2],yout2[0],yout2[1],yout2[2]);

var n_day = todaygetDay();

switch (n_day)

{

case 0:{

strDate = "星期日"

}break;

case 1:{

strDate = "星期一"

}break;

case 2:{

strDate ="星期二"

}break;

case 3:{

strDate = "星期三"

}break;

case 4:{

strDate = "星期四"

}break;

case 5:{

strDate = "星期五"

}break;

case 6:{

strDate = "星期六"

}break;

case 7:{

strDate = "星期日"

}break;

}

year = todaygetFullYear();

month = todaygetMonth() + 1;

date = todaygetDate();

hour = todaygetHours();

minute =todaygetMinutes();

second = todaygetSeconds();

if(month<10) month="0"+month;

if(date<10) date="0"+date;

if(hour<10) hour="0"+hour;

if(minute<10) minute="0"+minute;

if(second<10) second="0"+second;

documentgetElementById('divCurrentUser')value = "当前时间:" + year + "年" + month + "月" + date + "日 " + strDate +" " + hour + ":" + minute + ":" + second; //显示时间

setTimeout("getServerTime()",1000)

}

//判断参数是否闰年

function leapyear(year){

if(!/[^0]\d+/test(year)){

//通过正则判断年份的合法性

return false;

}

if(0==year)

return true;

if ( ((0==year%4) && !(0==year%100)) || (0==year%400) ){

return true;

}else{

if(year!=0)

return false;

}

}

</script>

初始化你的JLabel ,实例化new 就可以了。

同时你的程序有个比较严重的问题,那就是线程是否安全,swing不是线程安全的,你在一个新开的线程中更新GUI可能会报错,虽然这样的可能性很低,但是如果你要严谨的话,在你的线程中添加如下代码

SwingUtilitiesinvokeAndWait(new Runnable() {

public void run() {

displayAreasetText(dateFormatterformat(CalendargetInstance()getTime()));

}

});

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » C#获取服务器时间??急急急请高手帮忙..

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情