Get the Current Date and Time in Visual Basic.NET
1. Open Microsoft Visual Studio.
2. Click File > New > Web Site.
3. Select Visual Basic as the installed template and select ASP.NET Empty Web Site. Click OK.
4. In the solution explorer, right click on the project solution and click Add New Item.
5. Select Visual Basic as the installed template and select Web Form. Click OK.
6. Make sure you’re in Source view in Default.aspx. Inside the open <div> </div> tags enter a set of opening and closing <p> tags. What the <p> tag does is create a new paragraph.
7. In the Toolbox click and drag a Label tool inside the paragraph tags. Make sure to remove the Text from the label as it is not necessary because we are going to be inputting text into it when the button is clicked.
8. After the closing Label tag (</asp:Label>) enter the <br /> (break tag). What this does is cause a space in the paragraph, basically starting a new sentence in the paragraph.
9. In the Toolbox, click and drag a Button tool underneath the break tag we just created. Make sure to change the text to Date. The text is what will be on the button.
I just signed up at Server Intellect and couldn’t be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<asp:Label ID="Label1" runat="server" ></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Date1" />
</p>
</div>
</form>
</body>
</html>
10. Switch to Design view in Default.aspx. Double click on the button with the text Date.
You will see a Button1_Click method. This method handles the event of a user clicking on the Date button.
11. Inside the Button1_Click method we will enter code that will assign Label1’s text to equal the current date and time.
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim D As Date = Now()
Label1.Text = D
End Sub
End Class
We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.
Run the application by either clicking the green arrow at the top of the window in Microsoft Visual Studio or by simply pressing F5. It will ask you if you want to go into debug mode, click OK. You will see blank page with a button labeled Date. Click it and the current time and date will appear above it. Click the button a couple of times and notice the time changes to the current time.
Thats it! You have successfully displayed the date and time in Microsoft Visual Studio using Visual Basic. Showing the time is fairly basic, but a very important implementation for many websites and applications.
WebSite11.zip
