How to add Hours onto Date and Time in ASP.NET

The script below is a slight modification on our date and time with ordinal suffix snippet. The only difference is that on the Page_Load event the text property of the label 'lblDateTime' is set to the returned value of the Function ReturnDateTime which adds 10 hours onto the current time. The code displays the date and time in the format shown in figure 1.

Figure 1
Date Time Ordinal Suffix

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
     
 Function ReturnDateTime() As String

      Dim theDateTime As String = ""

      Dim NumericDayofMonth As Integer = Date.Now.Day
      Dim OrdinalSuffix As String = ""
     
     Select Case NumericDayofMonth
      Case 1, 21, 31
       OrdinalSuffix = "st"
      Case 2, 22
       OrdinalSuffix = "nd"
      Case 3, 23
       OrdinalSuffix = "rd"
      Case Else
       OrdinalSuffix = "th"
     End Select

      Dim NumericMonthofYear As Integer = Date.Now.Month
      Dim MonthofYear As String = ""
      
     Select Case NumericMonthofYear
      Case 1
       MonthofYear = "Jan"
      Case 2
       MonthofYear = "Feb"
      Case 3
       MonthofYear = "Mar"
      Case 4
       MonthofYear = "Apr"
      Case 5
       MonthofYear = "May"
      Case 6
       MonthofYear = "Jun"
      Case 7
       MonthofYear = "Jul"
      Case 8
       MonthofYear = "Aug"
      Case 9
       MonthofYear = "Sep"
      Case 10
       MonthofYear = "Oct"
      Case 11
       MonthofYear = "Nov"
      Case 12
       MonthofYear = "Dec"
     End Select

      Dim NumericDayofWeek As Integer = Date.Now.DayOfWeek
      Dim DayofWeek As String = ""

     Select Case NumericDayofWeek
      Case 1
        DayofWeek = "Monday"
       Case 2
        DayofWeek = "Tuesday"
       Case 3
        DayofWeek = "Wednesday"
       Case 4
        DayofWeek = "Thursday"
       Case 5
        DayofWeek = "Friday"
       Case 6
        DayofWeek = "Saturday"
       Case 7
        DayofWeek = "Sunday"
      End Select

     theDateTime &= DayofWeek & " " 'Monday, Tuesday, Wednesday etc
     theDateTime &= DateTime.Now.Day.ToString() '1, 2, 3, 4 etc
     theDateTime &= OrdinalSuffix & " " 'st, nd, rd, th
     theDateTime &= MonthofYear & " " 'Jan, Feb etc
     theDateTime &= DateTime.Now.Year.ToString() '2006, 2007 etc
     theDateTime &= DateTime.Now.AddHours(10).Hour.ToString & ":" 'Add 10 hours
     theDateTime &= DateTime.Now.Minute.ToString() & ":" 'minute
     theDateTime &= DateTime.Now.Second.ToString() 'seconds

     Return theDateTime
 End Function

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
     lblDateTime.Text = ReturnDateTime()
     End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Add 10 hours to Date and Time</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:Label ID="lblDateTime" runat="server" />
  </div>
</form>
</body>
</html>

Get the best asp hosting provider from web-hosting-top.com and save up to 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Global ASP.NET Hosting Leader - Click Here