当前位置:编程学习 > C#/ASP.NET >>

asp.net 如何获取服务器时间

获得本地时间很简单的几行代码,

protected void Page_Load(object sender, EventArgs e)
{


DateTime a;
a = System.DateTime.Now;
//Response.Write(a.Year);
//Response.Write(a.Month);
//Response.Write(a);
this.Label3.Text = a.ToLongDateString();
this.Label4.Text = a.ToShortTimeString();
如果是internet 的时间呢

补充:原来是这样,我是在本地测式,既做服务端也做客户端,所以把时间一改,也跟着变了
		
答案:帮你在网上找了点

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace AutoExitWindows
{
/// <summary>
/// ReSetLocalTimeFromNetwork 的摘要说明。
/// </summary>
public class ReSetLocalTimeFromNetwork
{
[DllImport("kernel32",SetLastError=true)]
   static extern int SetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("kernel32",SetLastError=true)]
   static extern int GetLastError();
//Public Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Long
//Public Declare Function SetSystemTime Lib "kernel32" Alias "SetSystemTime" (lpSystemTime As SYSTEMTIME) As Long
public struct SYSTEMTIME
{
short wYear;
short wMonth;
short wDayOfWeek;
short wDay;
short wHour;
short wMinute;
short wSecond;
short wMilliseconds;

public SYSTEMTIME(short year, short month, short dayofweek, short day, short hour, short minute, short second, short milliseconds)
{
wYear=year;
wMonth=month;
wDayOfWeek=dayofweek;
wDay=day;
wHour=hour;
wMinute=minute;
wSecond=second;
wMilliseconds=milliseconds;
}
}

public ReSetLocalTimeFromNetwork(string Server, string Port, string TimeZone)
{
try
{
//取网络时间
SNTPTimeClient client=new SNTPTimeClient(Server,Port);
client.Connect();
string strTest=client.ToString();
Console.Write(strTest);

//取出年、月、日、时、分、秒、微秒
short intYear, intMonth, intDayofWeek, intDay, intHour, intMinute, intSecond, intMilliseconds;

Char[] split=new Char[1];
split[0]=Convert.ToChar("\n");
string[] temp=strTest.Split(split,100);

string strDate="";
int intPos=-1;
for(int i=0; i<temp.Length-1; i++)
{
strDate=temp[i];
if(strDate.Substring(0,10)=="Local time")
{
intPos=1;
break;
}
else
Console.Write(strDate.Substring(0,10) + "\n");
}
if (intPos<0 )
{
throw new Exception("Can't get server time!");
}
strDate=strDate.Substring(11);

//设置本地时间
//注意,设置时,写入的时候应该是格林威治时间
DateTime dtmDate=Convert.ToDateTime(strDate);
dtmDate=dtmDate.AddHours((-1) * Convert.ToInt16(TimeZone));
intYear=Convert.ToInt16 (dtmDate.Year);
intMonth=Convert.ToInt16 (dtmDate.Month);
intDayofWeek=Convert.ToInt16 (dtmDate.DayOfWeek);
intDay=Convert.ToInt16 (dtmDate.Day);
intHour=Convert.ToInt16 (dtmDate.Hour);
intMinute=Convert.ToInt16 (dtmDate.Minute);
intSecond=Convert.ToInt16 (dtmDate.Second);
intMilliseconds=Convert.ToInt16 (dtmDate.Millisecond );

SYSTEMTIME systime=new SYSTEMTIME(intYear, intMonth, intDayofWeek, intDay, intHour, intMinute, intSecond, intMilliseconds);
int intValue=SetSystemTime(ref systime);
if (intValue==0)
{
intValue=GetLastError();
throw new Exception(intValue.ToString() + " error, can't set local time.");
}
//MessageBox.Show(intValue.ToString() + "\n" + DateTime.Now.ToString() + "\n" + strDate);
}
catch(Exception e1)
{
throw e1;
}
}

}
}

调用时:
ReSetLocalTimeFromNetwork objResetTime=new ReSetLocalTimeFromNetwork(strRemoteIP,strRemotePort,strTimeZone);


转载请标明出处: http://blog.csdn.net/limit/archive/2004/11/01/162436.aspx《===自己可以看看

上一个:asp.net的LISTBOX取值问题
下一个:ASP多数据库多重查询怎么弄?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,