开发者

Date & Time Separation

<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();
   开发者_Go百科         Label2.Text = inpHide1.Value.Trim();
            Label3.Text = Label2.Text;
}


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();
}


I use a string formats, as outlined on the C# Examples page from www.csharp-examples.net:

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


You should take a look at the msdn documentation around DateTime

You should pay particular attention to the parse function and the various ToString functions, being careful with which string formats you are passing in (or according to the current culture), to ensure you'll get the format your expecting.


I'm not sure which programming language you are using, but most have a function to break a string on whitespace. You could break the string this way and then take the date elements out and use them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜