Asp.net中的時區

來源:互聯網
上載者:User

首先,日期時間轉換在 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);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.