Asp.net中的时区

首先,日期时间转换在 Asp.net 中的几种应用:

1. <ASP:BOUNDCOLUMN DATAFIELD= "JoinTime " DATAFORMATSTRING= "{0:yyyy-MM-dd} " >    <ITEMSTYLE WIDTH= "18% "   >   </ITEMSTYLE >   </ASP:BOUNDCOLUMN > 
2.   e.Item.Cell[0].Text = Convert.ToDateTime(e.Item.Cell[0].Text).ToShortDateString(); 
3.   String.Format( "yyyy-MM-dd ",yourDateTime);       
4.   Convert.ToDateTime("2005-8-23").ToString("yyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo);   
5.   <asp:BoundColumn DataField="收款时间" HeaderText="收款时间" 
DataFormatString="{0:yyyy-mm-dd}">   </asp:BoundColumn> 
6.   DateTime.Now.ToString("yyyyMMddhhmmss") 
7.   DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") 
8.   只显示年月 
DataBinder.Eval(Container.DataItem,"starttime","{0:yyyy-M}") 
9.    <asp:BoundColumn DataField="收款时间" HeaderText="收款时间" 
DataFormatString="{0:yyyy-MM-dd HH24:mm:ss}">    </asp:BoundColumn>  

 

其次,系统中有效时区列表:

1. 根据本地时间取得时区列表:

        DateTimeOffset chinaDate = DateTimeOffset.Now; //本地当前时间
        Response.Write(chinaDate.ToString() + "<br/>");

        ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();//系统中地时区标识列表
        foreach (TimeZoneInfo timeZoneInfo in zones)
        {

            //通过本地时间取得格林威治标准时间,并通过这个标准时间取得不同时区ID的名称及它的相应时间
            DateTimeOffset easternDate = TimeZoneInfo.ConvertTime(chinaDate.UtcDateTime,
                TimeZoneInfo.FindSystemTimeZoneById(timeZoneInfo.Id));
            Response.Write(timeZoneInfo.Id + ":&nbsp;&nbsp;&nbsp;&nbsp;" 
                + easternDate.ToString() + "<br/>");
        }

       所得结果如下:

2008-9-12 14:33:12 +08:00
Greenwich Standard Time:     2008-9-12 6:33:12 +08:00
GMT Standard Time:     2008-9-12 7:33:12 +08:00
W. Central Africa Standard Time:     2008-9-12 7:33:12 +08:00
Romance Standard Time:     2008-9-12 8:33:12 +08:00
Central European Standard Time:     2008-9-12 8:33:12 +08:00
Central Europe Standard Time:     2008-9-12 8:33:12 +08:00
W. Europe Standard Time:     2008-9-12 8:33:12 +08:00
South Africa Standard Time:     2008-9-12 8:33:12 +08:00
E. Europe Standard Time:     2008-9-12 9:33:12 +08:00
Egypt Standard Time:     2008-9-12 9:33:12 +08:00
Israel Standard Time:     2008-9-12 8:33:12 +08:00
FLE Standard Time:     2008-9-12 9:33:12 +08:00
GTB Standard Time:     2008-9-12 9:33:12 +08:00
E. Africa Standard Time:     2008-9-12 9:33:12 +08:00
Arabic Standard Time:     2008-9-12 10:33:12 +08:00
Arab Standard Time:     2008-9-12 9:33:12 +08:00
Russian Standard Time:     2008-9-12 10:33:12 +08:00
Iran Standard Time:     2008-9-12 11:03:12 +08:00
Caucasus Standard Time:     2008-9-12 11:33:12 +08:00
Arabian Standard Time:     2008-9-12 10:33:12 +08:00
Afghanistan Standard Time:     2008-9-12 11:03:12 +08:00
West Asia Standard Time:     2008-9-12 11:33:12 +08:00
Ekaterinburg Standard Time:     2008-9-12 12:33:12 +08:00
India Standard Time:     2008-9-12 12:03:12 +08:00
Nepal Standard Time:     2008-9-12 12:18:12 +08:00
Sri Lanka Standard Time:     2008-9-12 12:33:12 +08:00
N. Central Asia Standard Time:     2008-9-12 13:33:12 +08:00
Central Asia Standard Time:     2008-9-12 12:33:12 +08:00
Myanmar Standard Time:     2008-9-12 13:03:12 +08:00
North Asia Standard Time:     2008-9-12 14:33:12 +08:00
SE Asia Standard Time:     2008-9-12 13:33:12 +08:00
North Asia East Standard Time:     2008-9-12 15:33:12 +08:00
China Standard Time:     2008-9-12 14:33:12 +08:00
Taipei Standard Time:     2008-9-12 14:33:12 +08:00
Singapore Standard Time:     2008-9-12 14:33:12 +08:00
W. Australia Standard Time:     2008-9-12 14:33:12 +08:00
Tokyo Standard Time:     2008-9-12 15:33:12 +08:00
Korea Standard Time:     2008-9-12 15:33:12 +08:00
Yakutsk Standard Time:     2008-9-12 16:33:12 +08:00
AUS Central Standard Time:     2008-9-12 16:03:12 +08:00
Cen. Australia Standard Time:     2008-9-12 16:03:12 +08:00
West Pacific Standard Time:     2008-9-12 16:33:12 +08:00
AUS Eastern Standard Time:     2008-9-12 16:33:12 +08:00
E. Australia Standard Time:     2008-9-12 16:33:12 +08:00
Vladivostok Standard Time:     2008-9-12 17:33:12 +08:00
Tasmania Standard Time:     2008-9-12 16:33:12 +08:00
Central Pacific Standard Time:     2008-9-12 17:33:12 +08:00
New Zealand Standard Time:     2008-9-12 18:33:12 +08:00
Fiji Standard Time:     2008-9-12 18:33:12 +08:00
Tonga Standard Time:     2008-9-12 19:33:12 +08:00
Azores Standard Time:     2008-9-12 6:33:12 +08:00
Cape Verde Standard Time:     2008-9-12 5:33:12 +08:00
Mid-Atlantic Standard Time:     2008-9-12 5:33:12 +08:00
E. South America Standard Time:     2008-9-12 3:33:12 +08:00
SA Eastern Standard Time:     2008-9-12 3:33:12 +08:00
Greenland Standard Time:     2008-9-12 4:33:12 +08:00
Newfoundland Standard Time:     2008-9-12 4:03:12 +08:00
SA Western Standard Time:     2008-9-12 2:33:12 +08:00
Pacific SA Standard Time:     2008-9-12 2:33:12 +08:00
Atlantic Standard Time:     2008-9-12 3:33:12 +08:00
Eastern Standard Time:     2008-9-12 2:33:12 +08:00
US Eastern Standard Time:     2008-9-12 1:33:12 +08:00
SA Pacific Standard Time:     2008-9-12 1:33:12 +08:00
Central America Standard Time:     2008-9-12 0:33:12 +08:00
Central Standard Time:     2008-9-12 1:33:12 +08:00
Canada Central Standard Time:     2008-9-12 0:33:12 +08:00
Mexico Standard Time:     2008-9-12 1:33:12 +08:00
US Mountain Standard Time:     2008-9-11 23:33:12 +08:00
Mountain Standard Time:     2008-9-12 0:33:12 +08:00
Mexico Standard Time 2:     2008-9-12 0:33:12 +08:00
Pacific Standard Time:     2008-9-11 23:33:12 +08:00
Alaskan Standard Time:     2008-9-11 22:33:12 +08:00
Hawaiian Standard Time:     2008-9-11 20:33:12 +08:00
Samoa Standard Time:     2008-9-11 19:33:12 +08:00
Dateline Standard Time:     2008-9-11 18:33:12 +08:00

 

其次,取得客户端时区偏移量(javascript):
    var zone = new Date().getTimezoneOffset()/60 * -1;

 

最后,取得在客户端应正确显示的时间:

            //serverZoneID 为数据库所在时区的ID,例如上面列表中的:“Eastern Standard Time”

            DateTimeOffset serverDateTime = TimeZoneInfo.ConvertTime(
            databaseDateTime, TimeZoneInfo.FindSystemTimeZoneById(serverZoneID));

            //zone 表示客户端时区偏移量

            serverDateTime.UtcDateTime.AddHours(zone);

posted @ 2009-06-25 11:07  Nina  阅读(683)  评论(0编辑  收藏  举报