ASP.Net/Language Basics/Date Time

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Assign Date and Integer value to asp:textbox without converting (VB.net)

<script language="vb" runat="server">
Sub Page_Load()
  Dim CapitalCityOfUK As String
  Dim NumberOfStates As Integer
  Dim IndependenceDay As Date
  CapitalCityOfUK = "London"
  NumberOfStates = 50
  IndependenceDay = #7/4/1863#
  Display1.Text = CapitalCityOfUK 
  Display2.Text = NumberOfStates
  Display3.Text = IndependenceDay
End Sub
</script>
<html>
<head>
<title>Creating Variables Example</title>
</head>
<body>
  The contents of CapitalCityOfUk is: 
  <asp:label id="Display1" runat="server" />
  <br>The contents of NumberOfStates is: 
  <asp:label id="Display2" runat="server" />
  <br>The contents of IndependenceDay is: 
  <asp:label id="Display3" runat="server" />
</body>
</html>



Assign date in form of "#3#" to asp:Label (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    
    Sub Button1_Click(sender As Object, e As EventArgs)
      Label1.Text = #3/6/2003#
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:Label id="Label1" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
        </p>
    </form>
</body>
</html>



Convert Current date to Long Date String and set to asp:Label (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub Button1_Click(sender As Object, e As EventArgs)
      Dim myDate As Date
      myDate = Now()
      Label1.Text = myDate.ToLongTimeString()
      Label2.Text = myDate.ToLongDateString()
      Label3.Text = myDate.ToShortTimeString()
      Label4.Text = myDate.ToShortDateString()
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:Label id="Label1" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label2" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label3" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label4" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
        </p>
    </form>
</body>
</html>



Convert Current date to Long Time String and set to asp:Label (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub Button1_Click(sender As Object, e As EventArgs)
      Dim myDate As Date
      myDate = Now()
      Label1.Text = myDate.ToLongTimeString()
      Label2.Text = myDate.ToLongDateString()
      Label3.Text = myDate.ToShortTimeString()
      Label4.Text = myDate.ToShortDateString()
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:Label id="Label1" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label2" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label3" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label4" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
        </p>
    </form>
</body>
</html>



Convert Current date to Short Date String and set to asp:Label (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub Button1_Click(sender As Object, e As EventArgs)
      Dim myDate As Date
      myDate = Now()
      Label1.Text = myDate.ToLongTimeString()
      Label2.Text = myDate.ToLongDateString()
      Label3.Text = myDate.ToShortTimeString()
      Label4.Text = myDate.ToShortDateString()
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:Label id="Label1" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label2" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label3" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label4" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
        </p>
    </form>
</body>
</html>



Convert Current date to Short Time String and set to asp:Label (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub Button1_Click(sender As Object, e As EventArgs)
      Dim myDate As Date
      myDate = Now()
      Label1.Text = myDate.ToLongTimeString()
      Label2.Text = myDate.ToLongDateString()
      Label3.Text = myDate.ToShortTimeString()
      Label4.Text = myDate.ToShortDateString()
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:Label id="Label1" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label2" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label3" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Label id="Label4" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
        </p>
    </form>
</body>
</html>



Convert text in asp textbox into Date (C#)

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load()
    {
        if (Page.IsPostBack) {
            DateTime MyDate;
            MyDate = Convert.ToDateTime(txtInput.Text);
            lblOut.Text = Convert.ToString(MyDate);
        }
    }
</script>
<html>
<head>
<title>Creating Variables Example</title>
</head>
<body>
    <form runat="server">
    Please enter a date, such as 10/31/2006
    <asp:TextBox runat="server" ID="txtInput">10/31/2006</asp:TextBox><br/>
    <asp:Button runat="server" Text="Button"></asp:Button><br/>
    Date is: 
    <asp:Label runat="server" ID="lblOut"/><br/>
    </form>
</body>
</html>



Date calculation: plus 7 days (C#)

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load()
    {
        if (Page.IsPostBack) {
            DateTime MyDate;
            MyDate = Convert.ToDateTime(txtInput.Text);
            MyDate = MyDate + TimeSpan.FromDays(7);
            lblOut.Text = Convert.ToString(MyDate);
        }
    }
</script>
<html>
<head>
<title>Creating Variables Example</title>
</head>
<body>
    <form runat="server">
    Please enter a date, such as 10/31/2006
    <asp:TextBox runat="server" ID="txtInput">10/31/2006</asp:TextBox><br/>
    <asp:Button runat="server" Text="Button"></asp:Button><br/>
    One week from the day you entered would be: 
    <asp:Label runat="server" ID="lblOut"/><br/>
    </form>
</body>
</html>



Date Comparison (C#)

<%@ Page Language="C#" debug="true"%>
<script runat="server">
    void Page_Load()
    {
        if (Page.IsPostBack)
        {
            if (Convert.ToDateTime(txtIn.Text) > Convert.ToDateTime("1/1/2000"))
            {
                lblOut.Text = "greater then 1/1/2000";
            }
            else
            {
                lblOut.Text = "equal or less then 1/1/2000";
            }
        }
    }
</script>
<html>
<head><title>Demonstrate Date Comparison</title></head>
<body>
    <form runat="server">
    Please enter a date before, equal to or after 1/1/2000:<br>
    <asp:TextBox runat="server" ID="txtIn"/><br/>
    <asp:Button runat="server" Text="Submit"/><br/>
    <asp:Label runat="server" ID="lblOut"/><br/>
    </form>
</body>
</html>



DateTime constructor: milli-seconds (C#)

<%@ Page Language="c#" %>
<script runat="server">
  void Page_Load() 
  {
    DateTime myDateTime1 = new DateTime();
    Response.Write(myDateTime1);
    Response.Write("<br />");
    DateTime myDateTime2 = new DateTime(1972,11,4);
    Response.Write(myDateTime2);
    Response.Write("<br />");
    DateTime myDateTime3 = new DateTime(1972,11,4,14,5,0);
    Response.Write(myDateTime3);
    Response.Write("<br />");
    DateTime myDateTime4 = new DateTime(260000000);
    Response.Write(myDateTime4);
    Response.Write("<br />");
  }
</script>



DateTime constructor: milli-second (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
  sub Page_Load()
    Dim myDateTime1 As New DateTime()
    Response.Write(myDateTime1)
    Response.Write("<br />")
    
    Dim myDateTime2 As New DateTime(1972,11,4)
    Response.Write(myDateTime2)
    Response.Write("<br />")
    
    Dim myDateTime3 As New DateTime(1972,11,4,14,5,0)
    Response.Write(myDateTime3)
    Response.Write("<br />")
    
    Dim myDateTime4 As New DateTime(260000000)
    Response.Write(myDateTime4)
    Response.Write("<br />")
  end sub
</script>



DateTime constructor: no parameters (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
  sub Page_Load()
    Dim myDateTime1 As New DateTime()
    Response.Write(myDateTime1)
    Response.Write("<br />")
    
    Dim myDateTime2 As New DateTime(1972,11,4)
    Response.Write(myDateTime2)
    Response.Write("<br />")
    
    Dim myDateTime3 As New DateTime(1972,11,4,14,5,0)
    Response.Write(myDateTime3)
    Response.Write("<br />")
    
    Dim myDateTime4 As New DateTime(260000000)
    Response.Write(myDateTime4)
    Response.Write("<br />")
  end sub
</script>



DateTime constructor: year, month, day (C#)

<%@ Page Language="c#" %>
<script runat="server">
  void Page_Load() 
  {
    DateTime myDateTime1 = new DateTime();
    Response.Write(myDateTime1);
    Response.Write("<br />");
    DateTime myDateTime2 = new DateTime(1972,11,4);
    Response.Write(myDateTime2);
    Response.Write("<br />");
    DateTime myDateTime3 = new DateTime(1972,11,4,14,5,0);
    Response.Write(myDateTime3);
    Response.Write("<br />");
    DateTime myDateTime4 = new DateTime(260000000);
    Response.Write(myDateTime4);
    Response.Write("<br />");
  }
</script>



DateTime constructor: year, month, day, hour, minute, second (C#)

<%@ Page Language="c#" %>
<script runat="server">
  void Page_Load() 
  {
    DateTime myDateTime1 = new DateTime();
    Response.Write(myDateTime1);
    Response.Write("<br />");
    DateTime myDateTime2 = new DateTime(1972,11,4);
    Response.Write(myDateTime2);
    Response.Write("<br />");
    DateTime myDateTime3 = new DateTime(1972,11,4,14,5,0);
    Response.Write(myDateTime3);
    Response.Write("<br />");
    DateTime myDateTime4 = new DateTime(260000000);
    Response.Write(myDateTime4);
    Response.Write("<br />");
  }
</script>



DateTime constructor: year, month, day, hour, minute, second (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
  sub Page_Load()
    Dim myDateTime1 As New DateTime()
    Response.Write(myDateTime1)
    Response.Write("<br />")
    
    Dim myDateTime2 As New DateTime(1972,11,4)
    Response.Write(myDateTime2)
    Response.Write("<br />")
    
    Dim myDateTime3 As New DateTime(1972,11,4,14,5,0)
    Response.Write(myDateTime3)
    Response.Write("<br />")
    
    Dim myDateTime4 As New DateTime(260000000)
    Response.Write(myDateTime4)
    Response.Write("<br />")
  end sub
</script>



DateTime constructor: year, month, day (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
  sub Page_Load()
    Dim myDateTime1 As New DateTime()
    Response.Write(myDateTime1)
    Response.Write("<br />")
    
    Dim myDateTime2 As New DateTime(1972,11,4)
    Response.Write(myDateTime2)
    Response.Write("<br />")
    
    Dim myDateTime3 As New DateTime(1972,11,4,14,5,0)
    Response.Write(myDateTime3)
    Response.Write("<br />")
    
    Dim myDateTime4 As New DateTime(260000000)
    Response.Write(myDateTime4)
    Response.Write("<br />")
  end sub
</script>



Get Hour, Second and Minute from Current time (VB.net)

<script language="vb" runat="server">
    Sub Page_Load()
       time.text=Hour(Now) & ":" & Minute(Now) & ":" & Second(Now)
    End Sub
</script>
<html>
<head><title>The Punctual Web Server</title></head>
<body>
      In WebServerLand the time is currently:
    <asp:label id="time" runat="server" />
</body>
</html>



Get Time Of a Day (VB.net)

<%@ Page Language=VB Debug=true %>
<%@ OutputCache Duration=30 VaryByParam=none%>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    lbl1.Text = "The current time on the server is " & TimeOfDay()
End Sub
</script>
<HTML>
<HEAD>
<TITLE>Using the OutputCache Directive</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<asp:label id="lbl1" runat="server"/>
</Form>
</BODY>
</HTML>



Get Today date (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    lblMessage1.Text = Today()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Manipulating a Label Control in Code</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
<asp:label 
    id="lblMessage1"
    tooltip="Today"s serverdate."
    runat="server"
/>
<BR><BR>
</form>
</BODY>
</HTML>



new DateTime() (C#)

<%@ Page Language="c#" %>
<script runat="server">
  void Page_Load() 
  {
    DateTime myDateTime1 = new DateTime();
    Response.Write(myDateTime1);
    Response.Write("<br />");
    DateTime myDateTime2 = new DateTime(1972,11,4);
    Response.Write(myDateTime2);
    Response.Write("<br />");
    DateTime myDateTime3 = new DateTime(1972,11,4,14,5,0);
    Response.Write(myDateTime3);
    Response.Write("<br />");
    DateTime myDateTime4 = new DateTime(260000000);
    Response.Write(myDateTime4);
    Response.Write("<br />");
  }
</script>



Time limit validation (C#)

<%@ Page Language="C#" %>
<script runat="server">
    void Page_Load()
    {
        if (!Page.IsPostBack)
            ResetStartTime();
    }
    
    void btnAgain_Click(Object sender, EventArgs e)
    {
        ResetStartTime();
    }
    
    void ResetStartTime()
    {
        Session["StartTime"] = DateTime.Now;
    }
    
    
    void valAnswer_ServerValidate(Object source, ServerValidateEventArgs args)
    {
        DateTime startTime = (DateTime)Session["StartTime"];
        if (startTime.AddSeconds(5) > DateTime.Now)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Timed Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <p>
    You have 5 seconds to answer the following question:
    </p>
    
    <asp:Label
        id="lblQuestion"
        Text="What was Aristotle"s first name?"
        AssociatedControlID="txtAnswer"
        Runat="server" />
    <br />
    <asp:TextBox
        id="txtAnswer"
        Runat="server" />    
    <asp:CustomValidator
        id="valAnswer"
        Text="(You answered too slowly!)"
        OnServerValidate="valAnswer_ServerValidate"
        Runat="server"  />
    
    <br /><br />
    
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        Runat="server" />
    
    <asp:Button
        id="btnAgain"
        Text="Try Again!"
        CausesValidation="false"
        OnClick="btnAgain_Click"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>



Year() function (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Date_ServerValidation(source As object, E As ServerValidateEventArgs)
    If  IsDate(E.Value) Then
        If  Year(E.Value) = Year(Today) Then
            E.IsValid = True
        Else
            E.IsValid = False
        End If
    Else
        E.IsValid = False
    End If
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Validating for a Date in the Current Year Using the CustomValidator Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter a date in the current year:<BR>
<asp:textbox 
    id="txtDate" 
    runat=server 
/>
<asp:customvalidator 
    id="customDate"
    controltovalidate="txtDate"
    onservervalidate="Date_ServerValidation"
    display="Dynamic"
    font-name="Verdana"
    font-bold="True"
    font-size="10pt"
    runat="server">
    You must enter a date in the current year!
</asp:CustomValidator>
<BR><BR>
<asp:button 
    id="butOK"
    text="OK"
    type="Submit"
    onclick="SubmitBtn_Click" 
    runat="server"
/>
</form>
</BODY>
</HTML>