2011-05-25 165 views
0
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <input id="inpHide" type="hidden" runat="server" /> 
     <input id="inpHide1" type="hidden" runat="server" /> 
     <asp:Label ID="Label1" runat="server"></asp:Label> 
     <asp:Label ID="Label2" runat="server"></asp:Label> 
     <asp:Label ID="Label3" runat="server"></asp:Label> 
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Width="100px" Height="30px" Text="Button" /> 
    </div> 
    <script language="javascript" type="text/javascript"> 
     var current_date = new Date(); 
     var current_timezone = current_date.getTimezoneOffset(); 
     document.getElementById("inpHide").value = current_timezone * -1; 
     document.getElementById("inpHide1").value = current_date; 
     </script> 
</body> 
</html> 

protected void Button1_Click(object sender, EventArgs e) 
{ 
     Label1.Text = inpHide.Value.Trim(); 
      Label2.Text = inpHide1.Value.Trim(); 
      Label3.Text = Label2.Text; 
} 
+7

這不是真的清楚你的輸入和輸出類型都在這裏。請你澄清一下嗎? – 2011-05-25 13:52:58

回答

5
DateTime dt; 
string Temp1 = "Your Date"; 
if (Temp1.LastIndexOf("GMT") > 0) 
{ 
    Temp1 = Temp1.Remove(Temp1.LastIndexOf("GMT")); 
} 
Temp1 = "Wed May 25 23:43:31 UTC+0900 2011"; 
if (Temp1.LastIndexOf("UTC") > 0) 
{ 
    Temp1 = Temp1.Remove(Temp1.LastIndexOf("UTC"), 9); 
    string[] split = Temp1.Split(' '); 
    Temp1 = split[0] + " " + split[1] + " " + split[2] + " " + split[4] + " " + split[3]; 
} 
if (DateTime.TryParse(Temp1, out dt)) 
{ 
    // If it is a valid date 
    string date = dt.ToShortDateString(); 
    string time = dt.ToShortTimeString(); 
} 
+0

string Temp1 =「2011年5月25日星期三19:15:49 GMT + 0530(印度標準時間)」; 從上面的字符串Temp1中,我需要單獨從Temp1中分離出「May 25 2011」。這樣我可以字符串Temp2 =「25/05/2011」; – Arun 2011-05-25 14:20:12

+0

您提到的格式應該在修改格林威治標準時間後的部分和最終驗證後進行準備。 – Pankaj 2011-05-25 14:27:14

+0

對不起,你的代碼工作不正常。在調試期間,函數不會進入「if」循環。 – Arun 2011-05-25 14:33:04

0

我不確定你正在使用哪種編程語言,但大多數函數都有一個函數來打破空白字符串。你可以通過這種方式打破字符串,然後取出日期元素並使用它們。

+0

你好@ astay13你說得對,它不是那麼清楚,但我相信Arun正在使用c#/ asp.net,正如它在問題標籤中所說的那樣。 – 2011-05-25 13:57:50

+0

@亞歷克斯,謝謝你指出。 :) – astay13 2011-05-25 14:09:31

+0

不用擔心,我總是想念自己的標籤! :-) – 2011-05-25 14:15:13

2

你應該看看msdn documentation around DateTime

你應該特別注意的解析功能和各種的ToString功能,小心使用你逝去其中字符串格式(或根據當前文化),以確保你會得到你期待的格式。

+0

謝謝,我會看到它.. – Arun 2011-05-25 14:33:49

+0

不用擔心@阿倫,我希望它有幫助。 – 2011-05-25 14:36:53

4

我使用一個字符串格式的C# Examples頁的說明從www.csharp-examples.net:

String.Format("{0:t}", dt); // "4:05 PM"       ShortTime 
String.Format("{0:d}", dt); // "3/9/2008"      ShortDate 
+0

我的代碼是string sample =「Wed May 25 23:43:31 UTC + 0900 2011」;上面的代碼string.format無法正常工作。 – Arun 2011-05-25 15:05:38

+1

簡單而完美! – 2014-08-06 20:44:56