ASP.Net/Language Basics/Date Time

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

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

   <source lang="csharp">

<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" />
 
The contents of NumberOfStates is: <asp:label id="Display2" runat="server" />
The contents of IndependenceDay is: <asp:label id="Display3" runat="server" />

</body> </html>

      </source>
   
  


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

   <source lang="csharp">

<%@ 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">

<asp:Label id="Label1" runat="server">Label</asp:Label>

<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>

   </form>

</body> </html>

      </source>
   
  


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

   <source lang="csharp">

<%@ 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">

<asp:Label id="Label1" runat="server">Label</asp:Label>

<asp:Label id="Label2" runat="server">Label</asp:Label>

<asp:Label id="Label3" runat="server">Label</asp:Label>

<asp:Label id="Label4" runat="server">Label</asp:Label>

<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>

   </form>

</body> </html>

      </source>
   
  


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

   <source lang="csharp">

<%@ 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">

<asp:Label id="Label1" runat="server">Label</asp:Label>

<asp:Label id="Label2" runat="server">Label</asp:Label>

<asp:Label id="Label3" runat="server">Label</asp:Label>

<asp:Label id="Label4" runat="server">Label</asp:Label>

<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>

   </form>

</body> </html>

      </source>
   
  


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

   <source lang="csharp">

<%@ 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">

<asp:Label id="Label1" runat="server">Label</asp:Label>

<asp:Label id="Label2" runat="server">Label</asp:Label>

<asp:Label id="Label3" runat="server">Label</asp:Label>

<asp:Label id="Label4" runat="server">Label</asp:Label>

<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>

   </form>

</body> </html>

      </source>
   
  


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

   <source lang="csharp">

<%@ 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">

<asp:Label id="Label1" runat="server">Label</asp:Label>

<asp:Label id="Label2" runat="server">Label</asp:Label>

<asp:Label id="Label3" runat="server">Label</asp:Label>

<asp:Label id="Label4" runat="server">Label</asp:Label>

<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>

   </form>

</body> </html>

      </source>
   
  


Convert text in asp textbox into Date (C#)

   <source lang="csharp">

<%@ 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>
<asp:Button runat="server" Text="Button"></asp:Button>
Date is: <asp:Label runat="server" ID="lblOut"/>
</form>

</body> </html>

      </source>
   
  


Date calculation: plus 7 days (C#)

   <source lang="csharp">

<%@ 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>
<asp:Button runat="server" Text="Button"></asp:Button>
One week from the day you entered would be: <asp:Label runat="server" ID="lblOut"/>
</form>

</body> </html>

      </source>
   
  


Date Comparison (C#)

   <source lang="csharp">

<%@ 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:
<asp:TextBox runat="server" ID="txtIn"/>
<asp:Button runat="server" Text="Submit"/>
<asp:Label runat="server" ID="lblOut"/>
</form>

</body> </html>

      </source>
   
  


DateTime constructor: milli-seconds (C#)

   <source lang="csharp">

<%@ Page Language="c#" %> <script runat="server">

 void Page_Load() 
 {
   DateTime myDateTime1 = new DateTime();
   Response.Write(myDateTime1);
   Response.Write("
"); DateTime myDateTime2 = new DateTime(1972,11,4); Response.Write(myDateTime2); Response.Write("
"); DateTime myDateTime3 = new DateTime(1972,11,4,14,5,0); Response.Write(myDateTime3); Response.Write("
"); DateTime myDateTime4 = new DateTime(260000000); Response.Write(myDateTime4); Response.Write("
"); }

</script>

      </source>
   
  


DateTime constructor: milli-second (VB.net)

   <source lang="csharp">

<%@ Page Language="VB" %> <script runat="server">

 sub Page_Load()
   Dim myDateTime1 As New DateTime()
   Response.Write(myDateTime1)
   Response.Write("
") Dim myDateTime2 As New DateTime(1972,11,4) Response.Write(myDateTime2) Response.Write("
") Dim myDateTime3 As New DateTime(1972,11,4,14,5,0) Response.Write(myDateTime3) Response.Write("
") Dim myDateTime4 As New DateTime(260000000) Response.Write(myDateTime4) Response.Write("
") end sub

</script>

      </source>
   
  


DateTime constructor: no parameters (VB.net)

   <source lang="csharp">

<%@ Page Language="VB" %> <script runat="server">

 sub Page_Load()
   Dim myDateTime1 As New DateTime()
   Response.Write(myDateTime1)
   Response.Write("
") Dim myDateTime2 As New DateTime(1972,11,4) Response.Write(myDateTime2) Response.Write("
") Dim myDateTime3 As New DateTime(1972,11,4,14,5,0) Response.Write(myDateTime3) Response.Write("
") Dim myDateTime4 As New DateTime(260000000) Response.Write(myDateTime4) Response.Write("
") end sub

</script>

      </source>
   
  


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

   <source lang="csharp">

<%@ Page Language="c#" %> <script runat="server">

 void Page_Load() 
 {
   DateTime myDateTime1 = new DateTime();
   Response.Write(myDateTime1);
   Response.Write("
"); DateTime myDateTime2 = new DateTime(1972,11,4); Response.Write(myDateTime2); Response.Write("
"); DateTime myDateTime3 = new DateTime(1972,11,4,14,5,0); Response.Write(myDateTime3); Response.Write("
"); DateTime myDateTime4 = new DateTime(260000000); Response.Write(myDateTime4); Response.Write("
"); }

</script>

      </source>
   
  


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

   <source lang="csharp">

<%@ Page Language="c#" %> <script runat="server">

 void Page_Load() 
 {
   DateTime myDateTime1 = new DateTime();
   Response.Write(myDateTime1);
   Response.Write("
"); DateTime myDateTime2 = new DateTime(1972,11,4); Response.Write(myDateTime2); Response.Write("
"); DateTime myDateTime3 = new DateTime(1972,11,4,14,5,0); Response.Write(myDateTime3); Response.Write("
"); DateTime myDateTime4 = new DateTime(260000000); Response.Write(myDateTime4); Response.Write("
"); }

</script>

      </source>
   
  


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

   <source lang="csharp">

<%@ Page Language="VB" %> <script runat="server">

 sub Page_Load()
   Dim myDateTime1 As New DateTime()
   Response.Write(myDateTime1)
   Response.Write("
") Dim myDateTime2 As New DateTime(1972,11,4) Response.Write(myDateTime2) Response.Write("
") Dim myDateTime3 As New DateTime(1972,11,4,14,5,0) Response.Write(myDateTime3) Response.Write("
") Dim myDateTime4 As New DateTime(260000000) Response.Write(myDateTime4) Response.Write("
") end sub

</script>

      </source>
   
  


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

   <source lang="csharp">

<%@ Page Language="VB" %> <script runat="server">

 sub Page_Load()
   Dim myDateTime1 As New DateTime()
   Response.Write(myDateTime1)
   Response.Write("
") Dim myDateTime2 As New DateTime(1972,11,4) Response.Write(myDateTime2) Response.Write("
") Dim myDateTime3 As New DateTime(1972,11,4,14,5,0) Response.Write(myDateTime3) Response.Write("
") Dim myDateTime4 As New DateTime(260000000) Response.Write(myDateTime4) Response.Write("
") end sub

</script>

      </source>
   
  


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

   <source lang="csharp">

<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>

      </source>
   
  


Get Time Of a Day (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Get Today date (VB.net)

   <source lang="csharp">

<%@ 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">

<asp:label

   id="lblMessage1"
   tooltip="Today"s serverdate."
   runat="server"

/>

</form> </BODY> </HTML>

      </source>
   
  


new DateTime() (C#)

   <source lang="csharp">

<%@ Page Language="c#" %> <script runat="server">

 void Page_Load() 
 {
   DateTime myDateTime1 = new DateTime();
   Response.Write(myDateTime1);
   Response.Write("
"); DateTime myDateTime2 = new DateTime(1972,11,4); Response.Write(myDateTime2); Response.Write("
"); DateTime myDateTime3 = new DateTime(1972,11,4,14,5,0); Response.Write(myDateTime3); Response.Write("
"); DateTime myDateTime4 = new DateTime(260000000); Response.Write(myDateTime4); Response.Write("
"); }

</script>

      </source>
   
  


Time limit validation (C#)

   <source lang="csharp">

<%@ 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">

You have 5 seconds to answer the following question:

   <asp:Label
       id="lblQuestion"
       Text="What was Aristotle"s first name?"
       AssociatedControlID="txtAnswer"
       Runat="server" />
   
<asp:TextBox id="txtAnswer" Runat="server" /> <asp:CustomValidator id="valAnswer" Text="(You answered too slowly!)" OnServerValidate="valAnswer_ServerValidate" Runat="server" />

<asp:Button id="btnSubmit" Text="Submit" Runat="server" /> <asp:Button id="btnAgain" Text="Try Again!" CausesValidation="false" OnClick="btnAgain_Click" Runat="server" />
   </form>

</body> </html>

      </source>
   
  


Year() function (VB.net)

   <source lang="csharp">

<%@ 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">

Enter a date in the current year:
<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>

<asp:button

   id="butOK"
   text="OK"
   type="Submit"
   onclick="SubmitBtn_Click" 
   runat="server"

/> </form> </BODY> </HTML>

      </source>