Results 1 to 4 of 4

Thread: Assistance with datediff required please

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    Re: Assistance with datediff required please

    Quote Originally Posted by techgnome View Post
    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???

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width