Results 1 to 7 of 7

Thread: last day of month

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    Israel the holy land
    Posts
    160

    last day of month

    how can i get the last day of a current month withought looping from 31 and down?
    thnaks i nadvance
    peleg
    Israel - the best place to live in after heaven, but no one want's to go there so fast.

    http://www.networked-toys.com/
    http://www.nirlat.com/home_page.asp

  2. #2
    Lively Member sishe's Avatar
    Join Date
    Aug 2004
    Location
    Izmit/Turkey
    Posts
    115

    Re: last day of month

    I think you can't get it with one line code or single function call.

    You must write a function for this or you can construct an array include every month's last day. But in both two ways you must consider leap years also.

    May be it is easier to find the next month's first day then step back 1 day by using dateadd function.
    Last edited by sishe; Mar 9th, 2005 at 04:38 AM.
    System Halted..
    -----------------------------------------
    Sishe

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: last day of month

    Like this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim a As Integer
    5.   Dim b As Integer
    6.   Dim c As Date
    7.   Dim lastday As Date
    8.   a = Month(Now) + 1
    9.   b = Year(Now)
    10.   If a = 13 Then a = 1: b = b + 1 ' Special code for December
    11.   c = Format(a & "/1/" & b, "mm/dd/yy")
    12.   lastday = DateAdd("d", c, -1)
    13.   MsgBox lastday & "  " & Format(lastday, "ddd")
    14. End Sub

    One more for the library
    Last edited by dglienna; Mar 9th, 2005 at 04:40 AM.

  4. #4
    Lively Member sishe's Avatar
    Join Date
    Aug 2004
    Location
    Izmit/Turkey
    Posts
    115

    Re: last day of month

    David you use Format function to set the date. What is the difference, in fact what is the advantages/disadvantages of using DateSerial function instead of Format?
    I have to use DateSerial function because in here people use different date formats, some use US (mm/dd/yy) but some use Turkish (dd/mm/yyyy). As a result I think I will have problems with Format. What do you think?
    System Halted..
    -----------------------------------------
    Sishe

  5. #5
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: last day of month

    another using dateserial.
    VB Code:
    1. Format(DateSerial(Year(Now), Month(Now) + 1, 0), "ddd")

    casey.

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: last day of month

    There really is no difference. Personal preference.
    format uses the format of the machine, you have to use that
    not an issue in the US, though

    Wasn't sure if you wanted the date or the day, so I included both.

    Casey, nice trick. Didn't think to use 0.

  7. #7
    Junior Member
    Join Date
    Apr 2005
    Location
    Indonesia
    Posts
    18

    Re: last day of month

    Try This One....

    VB Code:
    1. Function FindLastDay(ddate As Date) As Integer
    2.     ddate = DateAdd("d", 1 - Day(ddate), ddate)
    3.     FindLastDay = DatePart("d", DateAdd("d", -1, DateAdd("M", 1, ddate)))
    4. End Function
    Just BoundLess

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