Results 1 to 9 of 9

Thread: Dates...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Dates...

    Hello!

    I have two dates.

    For example:

    12.1.2009

    And

    15.7.2010

    How to print all dates between these two dates in one listbox, for example:

    12.1.2009
    13.1.2009
    14.1.2009
    ........
    14.7.2010
    15.7.2010

    Thanks!

  2. #2
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: Dates...

    Try something like this. Create a listbox on a form called lstDates. Then populate it with the code below on a click event of a button, for example. I am sure there are different ways to do this but here is a way I thought off of the top of my head.....
    Oh yeah I have the dates in USA format as M/DD/YYYY.

    Code:
    Dim startDate As DateTime = #1/12/2009#
    Dim endDate As DateTime = #7/15/2010#
    
            While startDate <= endDate
                Me.lstDates.Items.Add(startDate)
                startDate = startDate.AddDays(1)
            End While

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: Dates...

    And for format DD/M/YEAR?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Dates...

    the date format used will be according to your PC's regional settings, so you just need to change startDate + endDate to DD/M/YYYY format

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Dates...

    Code:
            'DD/M/YEAR
    
            'Dates are not stored as strings
            'but a string can be parsed into a date
            '12.1.2009
            'And
            '15.7.2010
            Dim ci As System.Globalization.CultureInfo = System.Globalization.CultureInfo.InvariantCulture
    
            Dim stdt, enddt As DateTime
    
            stdt = DateTime.ParseExact("12/1/2009", "dd/m/yyyy", ci)
            enddt = DateTime.ParseExact("15/7/2010", "dd/m/yyyy", ci)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: Dates...

    Code:
           Dim startDate As DateTime = "1/12/2009"
            Dim endDate As DateTime = "15/7/2010"
    
            While startDate <= endDate
                Me.lstDates.Items.Add(startDate)
                startDate = startDate.AddDays(1)
            End While
        End Sub
    This is the best way to do this. Thank you.

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Dates...

    Quote Originally Posted by hepeci View Post
    Code:
           Dim startDate As DateTime = "1/12/2009"
            Dim endDate As DateTime = "15/7/2010"
    
            While startDate <= endDate
                Me.lstDates.Items.Add(startDate)
                startDate = startDate.AddDays(1)
            End While
        End Sub
    This is the best way to do this. Thank you.
    Your best way won't compile.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    Lively Member
    Join Date
    Jan 2009
    Posts
    77

    Re: Dates...

    Quote Originally Posted by dbasnett View Post
    Your best way won't compile.
    It does, are you sure youve got the list box on your form?
    Whats the error?

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Dates...

    Add this as the very first line of your program:

    Option Strict On
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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