Reputation: 93
How to display full server date and time in vb.net ?
i want to display server date as 20-Nov-2010
in textbox1 and time as 08:11:00 AM
in Textbox2 on page load event
Upvotes: 2
Views: 12186
Reputation: 100358
If you want to display server time:
Markup:
<asp:TextBox runat="server" ID="TextBox1" />
<br />
<asp:TextBox runat="server" ID="TextBox2" />
Code-behind:
Imports System.Globalization
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' pay attention to DateTime.Today '
TextBox1.Text = DateTime.Today.ToString("dd-MMM-yyyy")
' culture-specific: '
Dim ci As CultureInfo = new CultureInfo("en-US")
TextBox2.Text = DateTime.Now.ToString("T", ci)
' or culture-independent: '
TextBox2.Text = DateTime.Now.ToString("hh:mm:ss tt")
End Sub
If you want to display client time:
Markup:
<!-- Adding the reference to jQuery CDN, for example, by Google -->
<script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<!-- Formatting function -->
<script type="text/javascript" language="javascript">
$(document).ready(function () {
var names = ["Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sepr", "Oct", "Nov", "Dec"];
var now = new Date();
$('#TextBox1').val(now.getDate() + '-' + names[now.getMonth()] + '-' + now.getFullYear());
var minutes = now.getMinutes();
$('#TextBox2').val(now.getHours() + ':' + formatLeadingZero(minutes) + ':' + formatLeadingZero(now.getSeconds()) + ' ' + formatTimePeriod(minutes));
});
function formatLeadingZero(value) {
return (value < 10) ? '0' + value : value;
}
function formatTimePeriod(value) {
return (value < 12) ? "AM" : "PM";
}
</script>
<!-- ASP.NET controls -->
<asp:TextBox runat="server" ID="TextBox1" />
<br />
<asp:TextBox runat="server" ID="TextBox2" />
Upvotes: 2
Reputation: 499352
Like this:
Response.Write(DateTime.Now.ToString())
Update: (following updated question)
Dim now As DateTime = DateTime.Now
textbox1.Text = now.ToString("dd-MMM-yyyy")
textbox2.Text = now.ToString("hh:mm:ss tt")
You may want to read up on custom DateTime format strings.
Upvotes: 1
Reputation: 8279
textbox1.Text = DateTime.Now.ToString("dd-MMM-yyyy")
textbox2.Text = DateTime.Now.ToString("hh:mm:ss tt")
Upvotes: 1