Bootstrap

js获取服务器当前时间

1.

<script type="text/javascript" language="javascript">
  var a='<%=day%>';
  alert(a);
    </script>

 

public static DateTime day;
    protected void Page_Load(object sender, EventArgs e)
    {
        day = DateTime.Now;
    }

 

2.

 

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>untitled</title>
<script type="text/javascript">
 get=function (id){return document.getElementById(id)}
 if(document.all){
     window.XMLHttpRequest=function(){
         var get=['Microsoft.XMLHTTP','Msxml2.XMLHTTP'];
   for(var i=0;i<get.length;i++){try{return new ActiveXObject(get[i])}catch(e){}};
     };
 }
 webDate=function(fn){
  var Htime=new XMLHttpRequest();
  Htime.onreadystatechange=function(){Htime.readyState==4&&(fn(new Date(Htime.getResponseHeader('Date'))))};
  Htime.open('HEAD', '/?_='+(-new Date));
  Htime.send(null);
 }
 window.time=new Date();
 targetTime=new Date();
 time2String=function (t){
   with(t)return [getFullYear(),'年'
    ,('0'+(getMonth()+1)).slice(-2),'月'
    ,('0'+getDate()).slice(-2),'日 '
    ,('0'+getHours()).slice(-2),': '
    ,('0'+getMinutes()).slice(-2),': '
    ,('0'+getSeconds()).slice(-2)].join('')
 }
 int2time=function (m){
  m-=(D=parseInt(m/86400000))*86400000;
  m-=(H=parseInt(m/3600000))*3600000;
  S=parseInt((m-=(M=parseInt(m/60000))*60000)/1000);
  return D+'天'+H+'小时'+M+'分'+S+'秒'
 }
 setInterval(function (){
  webDate(function (webTime){
   get('web').innerHTML=time2String(time=webTime);
  })
  get('locale').innerHTML=time2String(new Date);
  get('time').innerHTML=int2time(targetTime-time);
  if ((targetTime-time)<0) {
   get('time').innerHTML = 'Game Over';
  }
 },1000)
 </script> 
</head>
<body>
设定时间:2012年12月31日0时0分0秒<br>
服务器时间:<span id='web'>loading...</span><br>
本地时间:<span id="locale">loading...</span><br>
倒计时时间:<span id="time">loading...</span>
<script type="text/javascript" charset="utf-8">
 targetTime=new Date(2012,12,31,00,00,00);
</script>
</body>
</html>

 

3.

<script type="text/javascript">
        var lasttime=new Date(2011,0,7,9,5,0);
        var curtime=new Date();//   客户端   当前时间
        var diff=lasttime-curtime;//时间差,毫秒
        diff=Math.ceil(diff/1000);//换算成秒
        var interval;
        function counter(){
            if(diff>0){
                diff--;
                compute();
            }else{
                document.getElementById("ImgOne").style.display = "none";
                document.getElementById("ImgTwo").style.display = "block";
                document.getElementById("difftime").innerHTML="已结束";
                if(interval!="undefined"){
                    clearInterval(interval);
                }
            }
        }
        function compute(){
            var dd=Math.floor(diff / (24*60*60));
            var left=diff % (24*60*60);
            var hh=Math.floor(left/(60*60));
            left=left%(60*60)
            var mi=Math.floor(left/60);
            var ss=left%60;
            var difftime=document.getElementById("difftime");
            hh = dd*24+hh;
            difftime.innerHTML = hh + ":" + mi + ":" + ss ;
        };
        window.οnlοad=function(){
            interval=setInterval(counter,1000);
        };

    </script>

 

*****************************

 <!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 runat="server">
    <title>无标题页</title>

    <script type="text/javascript">
        var lasttime=new Date(2011,0,19,10,48,0);
        var curtime=new Date('<%=sYear %>','<%=sMonth %>','<%=sDay %>','<%=sHour %>','<%=sMinute %>','<%=sSecond %>');  
        var diff=lasttime-curtime;//时间差,毫秒
        diff=Math.ceil(diff/1000);//换算成秒
        var interval;
        function counter(){
            if(diff>0){
                diff--;
                compute();
            }else{
            
                document.getElementById("difftime").innerHTML="已结束";
                if(interval!="undefined"){
                    clearInterval(interval);
                }
            }
        }
        function compute(){
            var dd=Math.floor(diff / (24*60*60));
            var left=diff % (24*60*60);
            var hh=Math.floor(left/(60*60));
            left=left%(60*60)
            var mi=Math.floor(left/60);
            var ss=left%60;
            var difftime=document.getElementById("difftime");
            difftime.innerHTML = dd + "天" +hh + ":" + mi + ":" + ss ;

        };
        window.οnlοad=function(){
            interval=setInterval(counter,1000);
        };

    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            剩余时间:<span id="difftime"></span>
        </div>
    </form>
</body>
</html>

 

 public static int sYear, sMonth, sDay, sHour, sMinute, sSecond;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateTime dateTime = DateTime.Now;
            sYear = dateTime.Year;
            sMonth = dateTime.Month - 1;
            sDay = dateTime.Day;
            sHour = dateTime.Hour;
            sMinute = dateTime.Minute;
            sSecond = dateTime.Second;

        }
    }

 

 

4.

<script language="JavaScript" type="text/javascript">
            function count_down()
            {
                window.setTimeout("count_down()", 1000);

                testDay=new Date('<%=endTime %>');
                today=new Date();

                time=(testDay.getTime()-today.getTime())/(24*60*60*1000);
                day=Math.floor(time);
                time=(time-day)*24;
                hour=Math.floor(time);
                time=(time-hour)*60;
                minute=Math.floor(time);
                time=(time-minute)*60;
                second=Math.floor(time);

                show_time.innerHTML=day+"天"+hour+"小时"+minute+"分"+second+"秒";
            }
    </script>

 

<body οnlοad="count_down();">

 

<span id="show_time"
                                    runat="server" ></span>

 

 

 

 

.cs

string year = Convert.ToDateTime(dt.Rows[0]["EndTime"]).Year.ToString();
                string month = Convert.ToDateTime(dt.Rows[0]["EndTime"]).Month.ToString();
                string day = Convert.ToDateTime(dt.Rows[0]["EndTime"]).Day.ToString();
                string hour = Convert.ToDateTime(dt.Rows[0]["EndTime"]).Hour.ToString();
                string minute = Convert.ToDateTime(dt.Rows[0]["EndTime"]).Minute.ToString();
                string second = Convert.ToDateTime(dt.Rows[0]["EndTime"]).Second.ToString();
                endTime = year + "/" + month + "/" + "/" + day + " " + hour + ":" + minute + ":" + second;

;