display time in vb.net

newbie27
newbie27 used Ask the Experts™
on
Hello Experts
I am trying to show remaining time for the order as Delivery time in one of the user control on the website. The cut off time for the order during the day for example is set to 3. After this time it should display appropriate message in the banner. I have noticed that every time the minutes reaches 0, it errors  ex: 2 hrs 0 mins left
Also, when the remaining time is 0 hrs 0 mins, then it does not display anything at all.
It does not show 0 minutes and it errors. Please can someoen advise how can I handle this exception from the code attached?
Thanks

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim dnnbannertext As String = ""

            ' Dim timeremaining2 As DateTime = New DateTime(Now.Year, Now.Month, Now.Day, 23 - Now.Hour, 60 - Now.Minute, 0)
            Try
                If Not Page.IsPostBack Then

                End If
                If ((Now.Hour = 0 And Now.Minute > 0) Or (Now.Hour > 0)) And (Now.Hour < 15) Then
                    'timeremaining = 
                    Dim timeremaining As DateTime = New DateTime(Now.Year, Now.Month, Now.Day, 14 - Now.Hour, 60 - Now.Minute, 0)
                    dnnbannertext = "<div class='DeliveryBoxRow1 Light11 DBlue Pad2'>Orders placed within the next</div><div class='hrs Light16 White Centeralign'><b>" & timeremaining.Hour & " hrs and " & timeremaining.Minute & " min</b></div> "
                    dnnbannertext = dnnbannertext & "<div class='DBlue DeliveryBoxRow1 Light75'> Should arrive in :</div><div class='Blue DeliveryBoxRow1 Light12'> <strong>1-3 days</strong> -<span class='Blue'> Standard</span><br/> <strong>" & Now.AddDays(1).Day().ToString() & "th " & Now.AddDays(1).ToString("MMMM") & "</strong><span class='Blue'> - Express</span></div>"
                    mydnnBanner.InnerHtml = dnnbannertext
                ElseIf ((Now.Hour = 15 And Now.Minute > 0) Or (Now.Hour > 15)) And (Now.Hour < 23 Or (Now.Hour = 23 And Now.Minute < 59)) Then
                    dnnbannertext = "<div class='DeliveryBoxRow1 Light11 Blue Pad2'>Orders placed now will ship tomorrow.</div><div class='Blue DeliveryBoxRow1 Light75'> Should arrive in :</div><div class='Blue DeliveryBoxRow1 Light12'> <strong>1-3 days </strong>-<span class='Blue'> Standard</span><br/> <strong>" & Now.AddDays(1).Day().ToString() & "th " & Now.AddDays(1).ToString("MMMM") & "</strong><span class='Blue'> - Express</span></div>"
                    mydnnBanner.InnerHtml = dnnbannertext
                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2015

Commented:
This is due to your "60 - Now.Minute" usage. You are trying to pass 60 as a valid minute to a new DateTime. The upper limit on minutes is 59, because at 60, Hour would be incremented by 1. Modify your logic to prevent passing 60 as a minute value.
Most Valuable Expert 2011
Top Expert 2015

Commented:
>>  You are trying to pass 60 as a valid minute to a new DateTime.

By this I mean when Minute is equal to zero, you are passing 60 as a valid minute  :)

Author

Commented:
Hi kaufmed

Thanks for your input

Should I make this statement
 Dim timeremaining As DateTime = New DateTime(Now.Year, Now.Month, Now.Day, 14 - Now.Hour, 60 - Now.Minute, 0)

To

   Dim timeremaining As DateTime = New DateTime(Now.Year, Now.Month, Now.Day, 14 - Now.Hour, 59 - Now.Minute, 0)

Do you thinnk the 0 hrs 0 mins issue will also get resolved with this?
Thanks

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Commented:
A cleaner way to handle durations and time is using TimeSpan data type.
You could then rewrite your code as follows:

' Test only if time of day is before 3pm (since it can never be before 12am or after 11:59:59pm)
Dim limitTime As New TimeSpan(15, 0, 0)
If Now.TimeOfDay < limitTime Then
    Dim timeremaining As TimeSpan = limitTime.Subtract(Now.TimeOfDay)
    dnnbannertext = "<div class='DeliveryBoxRow1 Light11 DBlue Pad2'>Orders placed within the next</div><div class='hrs Light16 White Centeralign'><b>" & timeremaining.Hours & " hrs and " & timeremaining.Minutes & " min</b></div> "
    dnnbannertext = dnnbannertext & "<div class='DBlue DeliveryBoxRow1 Light75'> Should arrive in :</div><div class='Blue DeliveryBoxRow1 Light12'> <strong>1-3 days</strong> -<span class='Blue'> Standard</span><br/> <strong>" & Now.AddDays(1).Day().ToString() & "th " & Now.AddDays(1).ToString("MMMM") & "</strong><span class='Blue'> - Express</span></div>"
Else
    dnnbannertext = "<div class='DeliveryBoxRow1 Light11 Blue Pad2'>Orders placed now will ship tomorrow.</div><div class='Blue DeliveryBoxRow1 Light75'> Should arrive in :</div><div class='Blue DeliveryBoxRow1 Light12'> <strong>1-3 days </strong>-<span class='Blue'> Standard</span><br/> <strong>" & Now.AddDays(1).Day().ToString() & "th " & Now.AddDays(1).ToString("MMMM") & "</strong><span class='Blue'> - Express</span></div>"
End If
mydnnBanner.InnerHtml = dnnbannertext

Open in new window

Paul MacDonaldDirector, Information Systems

Commented:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim dnnbannertext As String = ""
            Dim dtNow As DateTime = Now()
            Dim dtExpriry As DateTime = New DateTime(Now.Year, Now.Month, Now.Day, 15, 0, 0)
            Dim dtTimeLeft As TimeSpan = dtExpriry - dtNow

            Try
                If Not Page.IsPostBack Then

                End If
                If (dtNow < dtExpriry) Then
                    'timeremaining = 
                    dnnbannertext = "<div class='DeliveryBoxRow1 Light11 DBlue Pad2'>Orders placed within the next</div><div class='hrs Light16 White Centeralign'><b>" & dtTimeLeft.Hour & " hrs and " & dtTimeLeft.Minute & " min</b></div> "
                    dnnbannertext = dnnbannertext & "<div class='DBlue DeliveryBoxRow1 Light75'> Should arrive in :</div><div class='Blue DeliveryBoxRow1 Light12'> <strong>1-3 days</strong> -<span class='Blue'> Standard</span><br/> <strong>" & Now.AddDays(1).Day().ToString() & "th " & Now.AddDays(1).ToString("MMMM") & "</strong><span class='Blue'> - Express</span></div>"
                    mydnnBanner.InnerHtml = dnnbannertext
                Else
                    dnnbannertext = "<div class='DeliveryBoxRow1 Light11 Blue Pad2'>Orders placed now will ship tomorrow.</div><div class='Blue DeliveryBoxRow1 Light75'> Should arrive in :</div><div class='Blue DeliveryBoxRow1 Light12'> <strong>1-3 days </strong>-<span class='Blue'> Standard</span><br/> <strong>" & Now.AddDays(1).Day().ToString() & "th " & Now.AddDays(1).ToString("MMMM") & "</strong><span class='Blue'> - Express</span></div>"
                    mydnnBanner.InnerHtml = dnnbannertext
                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

Open in new window

Author

Commented:
Hi Paulmacd

I have used your code and this is what I am getting as an error. Please can you advise?


Hi rafunk

I have used your error at this time I am not getting error but I have to change my watch slightly to see if it really works...

thanks all for your helps !
 	Error: Delivery is currently unavailable.
DotNetNuke.Services.Exceptions.ModuleLoadException: C:\Inetpub\wwwroot\vhosts\smithandgift.com\httpdocs\stage\DesktopModules\Banner\View.ascx.vb(34): error BC30456: 'Hour' is not a member of 'System.TimeSpan'. ---> System.Web.HttpCompileException: C:\Inetpub\wwwroot\vhosts\smithandgift.com\httpdocs\stage\DesktopModules\Banner\View.ascx.vb(34): error BC30456: 'Hour' is not a member of 'System.TimeSpan'. at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) at System.Web.Compilation.BuildManager.GetVPathBuildResult(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) at System.Web.UI.TemplateControl.LoadControl(VirtualPath virtualPath) at System.Web.UI.TemplateControl.LoadControl(String virtualPath) at DotNetNuke.UI.ControlUtilities.LoadControl[T](TemplateControl containerControl, String ControlSrc) in C:\CCNet_Builds\DotNetNuke_5_Maintenance\WorkingDirectory\Library\UI\ControlUtilities.vb:line 59 at DotNetNuke.UI.Modules.ModuleHost.LoadModuleControl() in C:\CCNet_Builds\DotNetNuke_5_Maintenance\WorkingDirectory\Library\UI\Modules\ModuleHost.vb:line 229 --- End of inner exception stack trace ---

Open in new window

Paul MacDonaldDirector, Information Systems
Commented:
Sorry - "dtTimeLeft.Hour" should be "dtTimeLeft.Hours" (note the trailing "s")
Commented:
1.)
If Now.Hour = 0 And Now.Minute > 0 Then

Statements like this can go terribly wrong.
DateTime.Now gets the current time and this may change from one call to the next, so you may get Now.Hour=0 at 0:59:59 and Now.Minute=0 a few mikroseconds later at 1:00:00

If you want to do calculation with a Date you have to ensure that it does not change during the calculation:

Dim CurrentDate = DateTime.Now

Then never use DateTime.Now for further caclulation, but only CurrentDate.

2.) "And" and "Or" are made for bit operations in VB.NET. Use the statements "AndAlso" and "OrElse" instead.

3.) What you are searching for is the Property "TimeOfDay" which gets a timespan of the current time only. Dont try to compare dates manually. This will only get you in trouble.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim dnnbannertext As String = ""
            Dim CurrentDate As DateTime = DateTime.Now
            Dim ExpiryTime As New TimeSpan(15,0,0)

            Try
                If Not Page.IsPostBack Then

                End If

                If CurrentDate.TimeOfDay<ExpiryTime Then
                    Dim Timeremaining = ExpiryTime - CurrentDate.TimeOfDay
                    dnnbannertext = "<div class='DeliveryBoxRow1 Light11 DBlue Pad2'>Orders placed within the next</div><div class='hrs Light16 White Centeralign'><b>" & dtTimeLeft.Hour & " hrs and " & dtTimeLeft.Minute & " min</b></div> "
                    dnnbannertext = dnnbannertext & "<div class='DBlue DeliveryBoxRow1 Light75'> Should arrive in :</div><div class='Blue DeliveryBoxRow1 Light12'> <strong>1-3 days</strong> -<span class='Blue'> Standard</span><br/> <strong>" & Now.AddDays(1).Day().ToString() & "th " & Now.AddDays(1).ToString("MMMM") & "</strong><span class='Blue'> - Express</span></div>"
                    mydnnBanner.InnerHtml = dnnbannertext
                Else
                    dnnbannertext = "<div class='DeliveryBoxRow1 Light11 Blue Pad2'>Orders placed now will ship tomorrow.</div><div class='Blue DeliveryBoxRow1 Light75'> Should arrive in :</div><div class='Blue DeliveryBoxRow1 Light12'> <strong>1-3 days </strong>-<span class='Blue'> Standard</span><br/> <strong>" & Now.AddDays(1).Day().ToString() & "th " & Now.AddDays(1).ToString("MMMM") & "</strong><span class='Blue'> - Express</span></div>"
                    mydnnBanner.InnerHtml = dnnbannertext
                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial