|
-
Jul 30th, 2010, 10:53 AM
#1
Thread Starter
Hyperactive Member
Assistance with datediff required please
Hi all.
I'm hoping someone can help me. I want to make a little program that will count down the hours to a specific date. I.E. "Hours until: ##/##/##"
Like they do for new year and other big events in the world or whatever but I'm having big problems. I don't know how to use datediff to compare the difference in hours from 1 date to the next. Any able to help
Much appreciated thanx
-
Jul 30th, 2010, 11:15 AM
#2
Re: Assistance with datediff required please
Don't use DateDiff ...
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim date1 As Date = New Date(2010, 12, 31, 23, 59, 59)
Dim date2 As Date = Date.Now
Dim hoursUntil As TimeSpan = date1 - date2
MessageBox.Show(String.Format("{0} hours until {1} {2}.", hoursUntil.TotalHours, date1.ToShortTimeString, date1.ToShortDateString))
'IF you want whole hours:
MessageBox.Show(String.Format("{0} hours, {1} minutes until {2} {3}.", (hoursUntil.Days * 24) + hoursUntil.Hours, hoursUntil.Minutes, date1.ToShortTimeString, date1.ToShortDateString))
End Sub
-tg
-
Jul 31st, 2010, 03:31 AM
#3
Thread Starter
Hyperactive Member
Re: Assistance with datediff required please
 Originally Posted by techgnome
Don't use DateDiff ...
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim date1 As Date = New Date(2010, 12, 31, 23, 59, 59)
Dim date2 As Date = Date.Now
Dim hoursUntil As TimeSpan = date1 - date2
MessageBox.Show(String.Format("{0} hours until {1} {2}.", hoursUntil.TotalHours, date1.ToShortTimeString, date1.ToShortDateString))
'IF you want whole hours:
MessageBox.Show(String.Format("{0} hours, {1} minutes until {2} {3}.", (hoursUntil.Days * 24) + hoursUntil.Hours, hoursUntil.Minutes, date1.ToShortTimeString, date1.ToShortDateString))
End Sub
-tg
TG thanks man!!!! Just one other thing, the date and time I wanted to specify is very many hours away lol and when I ran the code it didn't look too good (not cuz of the code, just the amount of hours), is there anyway I can adapt the code to show days,hours,minutes, seconds at all please???
-
Jul 31st, 2010, 04:29 PM
#4
Re: Assistance with datediff required please
The TimeSpan object has all of those... .Days, .Hours, .Minutes, .Seconds.... just look at the message box I posted... you can see how to use the string.format function to get the info and format it.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|