Results 1 to 19 of 19

Thread: [RESOLVED] how to display all dates of a given month?

  1. #1

    Thread Starter
    Member kirara22's Avatar
    Join Date
    Feb 2009
    Posts
    54

    Resolved [RESOLVED] how to display all dates of a given month?

    how to display all dates of a given month?

    textbox input :
    may 2009

    textbox output :

    may 1,2009
    may 2,2009
    may 3,2009
    ...
    until
    may 31,2009


    tnx advanz guys
    hope u help me..
    ..

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: how to display all dates of a given month?

    Try this
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub LoadDaysOfMonth(pintWhatMonth As Integer, _
    4.                             pintWhatYear As Integer, _
    5.                             pctrlWhatControl As Control)
    6.  
    7. Dim i As Integer
    8. Dim NumOfDays As Integer
    9.  
    10. Select Case pintWhatMonth
    11.     Case 1, 3, 5, 7, 8, 10, 12
    12.     NumOfDays = 31
    13.     Case 4, 6, 9, 11
    14.     NumOfDays = 30
    15.     Case 2
    16.     If IsThisALeapYear(pintWhatYear) Then
    17.         NumOfDays = 29
    18.     Else
    19.         NumOfDays = 28
    20.     End If
    21. End Select
    22.  
    23. For i = 1 To NumOfDays
    24.    pctrlWhatControl.AddItem pintWhatMonth & "/" & i & "/" & pintWhatYear
    25. Next
    26.  
    27. End Sub
    28.  
    29. Private Function IsThisALeapYear(ByVal ThisYear As Integer) As Boolean
    30.      IsThisALeapYear = (29 = Day(DateSerial(ThisYear, 2, 29)))
    31. End Function
    32.  
    33. Private Sub Command1_Click()
    34. LoadDaysOfMonth 5, 2009, Combo1
    35. End Sub
    Last edited by Hack; May 20th, 2009 at 06:55 AM.

  3. #3
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: how to display all dates of a given month?

    Code:
    Private Sub Command1_Click()
      Dim lX As Long
      Dim dStart As Date
      Dim dEnd As Date
      Dim dTemp As Date
      
      Text1.Text = Trim$(Text1.Text)
      Text2.Text = vbNullString
      
      If Not IsDate(Text1.Text) Then
        MsgBox "Not a date!"
        
        Exit Sub
      End If
      
      dStart = CDate(Format$("01." & Format$(Text1.Text, "MM.YYYY"), "DD.MM.YYYY"))
      dEnd = DateAdd("M", 1, dStart)
      
      For lX = 0 To DateDiff("D", dStart, dEnd) - 1
        dTemp = DateAdd("D", lX, dStart)
        
        If Len(Text2.Text) > 0 Then Text2.Text = Text2.Text & vbCrLf
        
        Text2.Text = Text2.Text & Format$(dTemp, "DD.MM.YYYY")
      Next lX
    End Sub

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: how to display all dates of a given month?

    You can achieve the same with much shorter code:
    Code:
    Private Sub Form_Load()
        Dim dtmNow As Date
        For dtmNow = Fix(Now) To DateSerial(Year(Now), Month(Now) + 1, 0)
            Debug.Print dtmNow
        Next dtmNow
    End Sub
    Last edited by Merri; May 20th, 2009 at 11:26 AM. Reason: Remove time from the date

  5. #5
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: how to display all dates of a given month?

    @Hack, why not use DateDiff and DateAdd. That way there would be no need for the check of LeapYear and the Select case.


    vb.net Code:
    1. Private Sub LoadDaysOfMonth(ByVal pintWhatMonth As Integer, _
    2.                             ByVal pintWhatYear As Integer, _
    3.                             ByVal pctrlWhatControl As Control)        
    4. Dim StartDate as Date
    5. Dim NumOfDays As Long
    6. StartDate="#/" & cstr(pintWhatMonth) & "/" & cstr(pintWhatYear) &"#"
    7. NumOfDays= DateDiff(DateInterval.Day, StartDate, DateAdd(DateInterval.Month, 1,StartDate))
    8. For i As Integer = 1 To NumOfDays
    9.     If TypeOf pctrlWhatControl Is ListBox Then
    10.         Dim tempListBox As ListBox = DirectCast(pctrlWhatControl, ListBox)
    11.         tempListBox.Items.Add(pintWhatMonth & "/" & i & "/" & pintWhatYear)
    12.     ElseIf TypeOf pctrlWhatControl Is ComboBox Then
    13.         Dim tempComboBox As ComboBox = DirectCast(pctrlWhatControl, ComboBox)
    14.         tempComboBox.Items.Add(pintWhatMonth & "/" & i & "/" & pintWhatYear)
    15.     End If
    16. Next
    17. End Sub
    18.  
    19. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    20.         LoadDaysOfMonth(5, 2009, ComboBox1)
    21. End Sub
    Last edited by opus; May 20th, 2009 at 07:12 AM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: how to display all dates of a given month?

    Wow! So many ways to do a simple thing.
    Code:
    Sub ListDaysInMonth()
        Dim iMonth As Integer
        Dim iYear As Integer
        Dim FirstDayOfMonth As Date
        Dim LastDayOfMonth As Date
        Dim dDate As Date
        
        iMonth = InputBox("Enter a Month number (1 to 12):")
        iYear = InputBox("Enter a 4-digit Year:")
        
        FirstDayOfMonth = DateSerial(iYear, iMonth, 1)
        LastDayOfMonth = DateSerial(iYear, iMonth + 1, 0)
        
        For dDate = FirstDayOfMonth To LastDayOfMonth
            Debug.Print dDate
        Next
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    Baby Steps a guided tour
    IsDigits() and IsNumber() functions Wichmann-Hill Random() function >> and << functions for VB CopyFileByChunk

  7. #7
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,355

    Re: how to display all dates of a given month?

    Code:
    Private Sub Command1_Click()
    Dim MyDays As Date
    For MyDays = DateSerial(2009, 5, 1) To DateSerial(2009, 5, 31)
        List1.AddItem MyDays
    Next
    End Sub
    This is similar to Merri's but starts at the first day of the month rather than the current day of the month. I sent the values to a listbox.
    Doctor Ed

  8. #8

    Thread Starter
    Member kirara22's Avatar
    Join Date
    Feb 2009
    Posts
    54

    Re: how to display all dates of a given month?

    merri 's suggestion is not working well in leap year ^^,
    but anyway tnx...

    well

    tnx among all of you guys.. u help me a lot!!!

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] how to display all dates of a given month?

    kirara22: why it wouldn't work? Leap years are handled by VB / system so giving the day zero of the next month always gives the last day of the current month.

    In the sample code I originally posted I forgot to remove the time of the day. I have updated the code (Now -> Fix(Now)) and now it prints also the last date of the month. This is what you get when you post in a train and don't have a VB on that machine for testing...

  10. #10

    Thread Starter
    Member kirara22's Avatar
    Join Date
    Feb 2009
    Posts
    54

    Re: [RESOLVED] how to display all dates of a given month?

    ok i c

    thank you so much !!!

    it works perfectly!!!

  11. #11
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [RESOLVED] how to display all dates of a given month?

    @Merri,
    Why use
    dtmNow = Fix(Now)
    while you can have
    dtmNow = Date

    Noted that: Now = Date + Time
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    Baby Steps a guided tour
    IsDigits() and IsNumber() functions Wichmann-Hill Random() function >> and << functions for VB CopyFileByChunk

  12. #12
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] how to display all dates of a given month?

    I keep forgetting that, because to me Date = Date datatype. Overlapping names are evil.

  13. #13
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: [RESOLVED] how to display all dates of a given month?

    Merri, your code does not even answer the question. It only provides dates from current date to the end of the month. But not all days in the month.

  14. #14
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] how to display all dates of a given month?

    That is because I wasn't attempting to do exactly that. It is easy enough to print out days once you know how to get the range. Thus I simply wanted to post a code to show how to get the last day of the month with one line of code. And that I did.

  15. #15
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: [RESOLVED] how to display all dates of a given month?

    Code Doc
    You code has a pro!,
    Not all Months have 31days,so you to put DaysInmonth
    Code:
    Private Sub display_dates_of_month(theYear, theMonth, DayInMonth)
    Dim MyDays As Date
    'For MyDays = DateSerial(2009, 4, 1) To DateSerial(2009, 4, 30)
    For MyDays = DateSerial(theYear, theMonth, 1) To DateSerial(theYear, theMonth, DaysInmonth)
        List1.AddItem MyDays
    Next
    End Sub
    So this would be the same
    Code:
    Private Sub display_dates_of_month(theYear, theMonth, DayInMonth)
    Dim I
    For I = 1 To DayInMonth
        List1.AddItem theMonth & "/" & I & "/" & theYear
    Next
    End Sub

  16. #16
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] how to display all dates of a given month?

    First day of the current month: DateSerial(Year(Now), Month(Now), 1)
    Last day of the current month: DateSerial(Year(Now), Month(Now) + 1, 0)

    With these you don't need to figure out how many days each month has. VB does it for you.


    Code:
    Private Sub display_dates_of_month(theYear, theMonth)
        Dim dtmI As Date
        List1.Clear
        For dtmI = DateSerial(theYear, theMonth, 1) To DateSerial(theYear, theMonth + 1, 0)
            List1.AddItem dtmI
        End Sub
    End Sub
    No need to pass how many days the month has to get it working correctly.

  17. #17
    Junior Member
    Join Date
    Jul 2013
    Posts
    25

    Re: [RESOLVED] how to display all dates of a given month?

    Hello,

    I am currently working on a project and I need a little help.

    I have a combo box which I will use to select the month.
    Once I select the month, I want the labels(control array) below to automatically populate with the dates of the month.

    I am trying to use nested For statements for the same but am unable to figure out the right way.

    any help on this would be greatly appreciated.

  18. #18
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,484

    Re: [RESOLVED] how to display all dates of a given month?

    prm...
    Welcome to the VB6 and Earlier forum....are you using VB6?
    Also, how about starting your own NEW thread, instead of bringing up an old 2009 one?
    What you are asking is very simple....but, there are already controls available in VB6 for you to do this instead of comboboxes....
    how about, in a new thread, reposting your question, and including what code you already have....?

  19. #19
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,484

    Re: [RESOLVED] how to display all dates of a given month?

    IF you don't want to start a new thread (or don't know how)....play with this example: Has two ways.....using a combobox and a monthview
    On a new project, add a monthview from components (MS Common Controls 2.0), a combobox, and label (set the label's index property to 0 and visible property to false). You can see how much easier it is using a monthview (of course, using a monthview, you really don't need to make a bunch of labels). However, I am guessing you are new to VB6 and are trying to learn....
    BUT, still, it is better to start a new thread.
    Code:
    Option Explicit
    
    Dim mArray() As String, Months(12) As String
    
    Private Sub Combo1_Click()
    
    Dim x As Integer, mMonth As String, mDays As Integer, y As Integer
    If Combo1.Text <> "MONTHS" Then
    For x = 1 To 31
       Label1(x).Visible = False
    Next x
    For x = 1 To 12
        mArray = Split(Months(x), " ")
        mMonth = mArray(0)
        mDays = CInt(mArray(1))
        If mMonth = Combo1.Text Then
            For y = 1 To mDays
    
                Label1(y).Caption = y
                Label1(y).Visible = True
            Next y
        End If
    Next x
    
    End If
    End Sub
    
    
    
    Private Sub Form_Load()
    
    Months(1) = "JANUARY 31"
    Months(2) = "FEBRUARY 28"
    Months(3) = "MARCH 31"
    Months(4) = "APRIL 30"
    Months(5) = "MAY 31"
    Months(6) = "JUNE 30"
    Months(7) = "JULY 31"
    Months(8) = "AUGUST 31"
    Months(9) = "SEPTEMBER 30"
    Months(10) = "OCTOBER 31"
    Months(11) = "NOVEMBER 30"
    Months(12) = "DECEMBER 31"
    
    Dim y As Integer
    For y = 1 To 31
        Load Label1(y)
        Label1(y).Top = Label1(y - 1).Top + Label1(y - 1).Height
    
    Next y
    Dim x As Integer, mMonth As String, mDays As Integer
    For x = 1 To UBound(Months) - 1
        mArray = Split(Months(x), " ")
        mMonth = mArray(0)
        mDays = CInt(mArray(1))
        Combo1.AddItem (mMonth)
    
    Next x
    Combo1.Text = "MONTHS"
    
    End Sub
    
    Private Sub MonthView1_Click()
    Dim dhDaysInMonth As Integer, y As Integer, myDate As String
    myDate = CStr(MonthView1.Month) & "/" & CStr(MonthView1.Day) & "/" & CStr(MonthView1.Year)
    For y = 1 To 31
       Label1(y).Visible = False
    Next y
        dhDaysInMonth = DaysInMonth(myDate)
         For y = 1 To dhDaysInMonth
                Label1(y).Caption = y
                Label1(y).Visible = True
         Next y
    End Sub
    Public Function DaysInMonth(ByVal dDate As Date) As Integer
        DaysInMonth = Day(DateAdd("m", 1, dDate - Day(dDate) + 1) - 1)
    End Function

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