Page 1 of 2 12 LastLast
Results 1 to 40 of 42

Thread: Days

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2004
    Posts
    69

    Resolved Days

    DEAR ALL,
    HOW CAN I GET THE NO OF DAYS IN A MONTH ?. iS THERE ANY SIMPLE FUNCTION OR FORMULA IN VB ?.



    THANK YOU,


    MA SULAIMAN
    Last edited by AHMEDSULAIMAN; Mar 27th, 2005 at 04:59 AM.

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

    Re: Days

    This will print the last day of the current month:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Debug.Print DateSerial(Year(Now), Month(Now) + 1, -1)
    5. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2004
    Posts
    69

    Re: Days

    THIS GIVES 30-MAR-2005.
    i WANT TO PRINT NO OF DAYS IN A MONTH

    THANK yOU,




    wITH BEST REGARDS,
    M A SULAIMAN.

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

    Re: Days

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   dim ld as date
    5.   ld =  DateSerial(Year(Now), Month(Now) + 1, 0)
    6.   msgbox day(ld) & " days in the month"
    7. End Sub

    Sorry, I had one too many subtracted

    gives you the last day of the month

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2004
    Posts
    69

    Resolved Re: Days

    Thank You very much.

  6. #6
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306

    Re: Days

    Quote Originally Posted by AHMEDSULAIMAN
    DEAR ALL,
    HOW CAN I GET THE NO OF DAYS IN A MONTH ?. iS THERE ANY SIMPLE FUNCTION OR FORMULA IN VB ?.



    THANK YOU,


    MA SULAIMAN
    Sulaiman, do u want to find out the number of days between two date's ?

    Then you have to use the DateDiff Function: Here's an example;

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim DT as Variant
    3. DT = DateDiff("d", txtDate1.Text, txtDate2.Text)
    4. txtTotDays.Text = Format(DT, "00")
    5. End Sub
    Current Project: General Employees Database (Employees Information) & (Training Details).

  7. #7
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Days

    Quote Originally Posted by AHMEDSULAIMAN
    Thank You very much.
    Edit your first post and add [RESOLVED] to your title or just put check icon
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

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

    Re: Days

    He wanted number of days in a month.

    Your example is completely wrong. How does two dates tell you the number of days in a month? Put in today and tomorrow. dt=1 not number of days in a month.

    You have to read the request, and then post a solution to that problem.
    Last edited by dglienna; Mar 27th, 2005 at 08:51 PM.

  9. #9
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Days

    Quote Originally Posted by Habibi
    Sulaiman, do u want to find out the number of days between two date's ?

    Then you have to use the DateDiff Function: Here's an example;

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim DT as Variant
    3. DT = DateDiff("d", txtDate1.Text, txtDate2.Text)
    4. txtTotDays.Text = Format(DT, "00")
    5. End Sub
    Hey Habibi. I was about to do something like that in my project. Thanks for the code. Saves me time figuring it out how to do it.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  10. #10
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306

    Re: Days

    u most welcome.
    Current Project: General Employees Database (Employees Information) & (Training Details).

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    In general, when a math function can do something locally we do that.

    In my opinion, it's better to have a CASE statement with the month number and the number of days in that month.

    It's extremely easy to determine the LEAP YEAR and see if FEB has 28 or 29 days.

    Calling a series of date functions with a neat little trick of add a month, subtract a day works - but the overhead is too much for me to handle.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Days

    Quote Originally Posted by szlamany
    Calling a series of date functions with a neat little trick of add a month, subtract a day works - but the overhead is too much for me to handle.
    Can you expand your idea?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    Quote Originally Posted by Mc Brain
    Can you expand your idea?
    An array like this:

    Code:
                Months(1) = 31
                Months(2) = 29
                Months(3) = 31
                Months(4) = 30
                Months(5) = 31
                Months(6) = 30
                Months(7) = 31
                Months(8) = 31
                Months(9) = 30
                Months(10) = 31
                Months(11) = 30
                Months(12) = 31
    ...later on in code, we take the value of the MONTH characters in the date string...

    Code:
                If month < 1 Or month > 12 Then
                    errorFlag = "This is not a valid month (enter dates as MMDDYYYY)"
                    Exit Function
                End If
                If month = 2 Then
                    If year / 100 = year \ 100 Then ' we have century
                       If year / 400 = year \ 400 Then ' and a leap year
                           MCO = 0
                       Else
                           MCO = 1
                       End If
                    Else
                       If year / 4 = year \ 4 Then ' not on century and we have a leap year
                           MCO = 0
                       Else
                           MCO = 1
                       End If
                    End If
                Else
                    MCO = 0
                End If
    
                If day < 1 Or day > Months(month) - MCO Then
                    errorFlag = "This is not a valid day (enter dates as MMDDYYYY)"
                    Exit Function
                End If
    The reason we do all this is that this is buried deep in a "general data validation" routine in our app's. We allow users to enter without SLASHES (actually prefer it) and then put the slashes in for visual aid after validation.

    This was a lot of cut/paste from an old source code on my home PC - hopefully it's clean!

    [edit] This might look like a lot of code, but in reality it's just a couple of IF statements and compiles into very fast machine code - much different then calling DATE functions that probably call API's

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Days

    Quote Originally Posted by szlamany
    An array like this:

    Code:
                Months(1) = 31
                Months(2) = 29
                Months(3) = 31
                Months(4) = 30
                Months(5) = 31
                Months(6) = 30
                Months(7) = 31
                Months(8) = 31
                Months(9) = 30
                Months(10) = 31
                Months(11) = 30
                Months(12) = 31
    ...later on in code, we take the value of the MONTH characters in the date string...

    Code:
                If month < 1 Or month > 12 Then
                    errorFlag = "This is not a valid month (enter dates as MMDDYYYY)"
                    Exit Function
                End If
                If month = 2 Then
                    If year / 100 = year \ 100 Then ' we have century
                       If year / 400 = year \ 400 Then ' and a leap year
                           MCO = 0
                       Else
                           MCO = 1
                       End If
                    Else
                       If year / 4 = year \ 4 Then ' not on century and we have a leap year
                           MCO = 0
                       Else
                           MCO = 1
                       End If
                    End If
                Else
                    MCO = 0
                End If
    
                If day < 1 Or day > Months(month) - MCO Then
                    errorFlag = "This is not a valid day (enter dates as MMDDYYYY)"
                    Exit Function
                End If
    The reason we do all this is that this is buried deep in a "general data validation" routine in our app's. We allow users to enter without SLASHES (actually prefer it) and then put the slashes in for visual aid after validation.

    This was a lot of cut/paste from an old source code on my home PC - hopefully it's clean!

    [edit] This might look like a lot of code, but in reality it's just a couple of IF statements and compiles into very fast machine code - much different then calling DATE functions that probably call API's
    I had understood your idea... but I still don't understand the statement
    "Calling a series of date functions with a neat little trick of add a month, subtract a day works - but the overhead is too much for me to handle"
    Besides, why do you do year / 400 = year \ 400 instead of (year MOD 400) = 0? Is it also an overhead issue?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    Quote Originally Posted by Mc Brain
    I had understood your idea... but I still don't understand the statement

    Quote:
    "Calling a series of date functions with a neat little trick of add a month, subtract a day works - but the overhead is too much for me to handle"

    Besides, why do you do year / 400 = year \ 400 instead of (year MOD 400) = 0? Is it also an overhead issue?
    I did not write that logic - someone on my staff did - so I cannot answer the MOD question.

    As for the general overhead feeling. If I had some time today I would benchmark the two and see what the difference is - my feeling is that the calls to DATE functions would be much, much slower than the couple of IF statements and DIVISION logic show in my post.

    I always consider how things will compile into machine instructions. Those IF and DIVISION statements in VB compile down into a dozen, if that, COMPARE, DIVIDE and JUMP operations. And it's all in-line with the VB code it's sitting in.

    Calling an OUTSIDE function probably has a dozen instructions just to prepare for the call. Passing arguments - framing the stack so the DATE function can run - handling results in temporary space (since the DATE logic has functions within functions).

    This matters to us because we use these standard routines of our to potentially process thousands and thousands of data elements - for report generation, filling grids, maybe importing from text files.

    All this is buried in a CHANGEDATATYPE function - which is responsible for validation and transformation from "stored" to "input" to "display" formats for all our different datatypes.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  16. #16
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Days

    Ok, if do your tests keep me posted. I would like to know the results.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  17. #17
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Days

    Two more things:

    VB Code:
    1. If year / 100 = year \ 100 Then ' we have century
    2.                    If year / 400 = year \ 400 Then ' and a leap year
    3.                        MCO = 0
    4.                    Else
    5.                        MCO = 1
    6.                    End If
    7.                 Else
    8.                    If year / 4 = year \ 4 Then ' not on century and we have a leap year
    9.                        MCO = 0
    10.                    Else
    11.                        MCO = 1
    12.                    End If
    13.                 End If

    First, what does MCO stand for?
    Second, I think it could be...

    VB Code:
    1. If year / 400 = year \ 400 Then ' and a leap year
    2. 'can be divided by 100 and 4
    3.                        MCO = 0
    4.                    Else
    5. 'cannot be divided by 100 or 4
    6.                       If year / 4 = year \ 4 Then ' not on century and we have a leap year
    7.                           MCO = 0
    8.                      Else
    9.                          MCO = 1
    10.                      End If
    11.                    End If
    However... I've not tested this.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  18. #18
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    Centuries have to be divisible by 400 - right? So 1900 is not a leap year (it does divide by 4 though) - 2000 is a leap year.

    If not a century, then if it divides by 4 - right?

    I'm sure MCO stands for something like MONTH-CHECK-OFFSET - but like I said, I did not write that code...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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

    Re: Days

    I'd like to see the speed comparison, but as I have said before, I prefer easy to read over code that is cryptic, but saves a few cycles. VB isn't known for it's speed anyways. You can speed it up somewhat, and that's what you are doing.

  20. #20
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Days

    Quote Originally Posted by Mc Brain
    Ok, if do your tests keep me posted. I would like to know the results.
    This is interesting. Keep me posted too. I'd like to know how to enter dates without SLASHES.

    Quote Originally Posted by szlamany
    The reason we do all this is that this is buried deep in a "general data validation" routine in our app's. We allow users to enter without SLASHES (actually prefer it) and then put the slashes in for visual aid after validation.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  21. #21
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    Ok - I used a link from MartinLiss about timing items - here's the code I used.

    Code:
    Option Explicit
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Private Sub Form_Load()
    
        ' Place this code in any Sub or Function
        Dim lngStart As Long
        Dim lngFinish As Long
        Dim lngCounterOne As Long
        Dim lngCounterTwo As Long
        Dim lngMonthCount As Long
        Dim Months(1 To 12) As Long
        Dim lngMonth As Long
        Dim lngYear As Long
        Dim mco As Long
        
        'Dim ld As Date
        Dim CheckDate1 As Date
        Dim CheckDate2 As Date
        
        Dim strDate1 As String
        Dim strDate2 As String
        
        CheckDate1 = "2004-01-15"
        CheckDate2 = "2004-02-15"
        
        ' Record the start "time"
        lngStart = GetTickCount()
        
        ' Some process that you want to time
        For lngCounterOne = 1 To 100000
            For lngCounterTwo = 1 To 5
                lngMonthCount = Day(DateSerial(Year(CheckDate1), Month(CheckDate1) + 1, 0))
                lngMonthCount = Day(DateSerial(Year(CheckDate2), Month(CheckDate2) + 1, 0))
            Next lngCounterTwo
        Next lngCounterOne
        
        ' Record the finish "time"
        
        lngFinish = GetTickCount()
        
        ' Display the difference
        
        Debug.Print "Time to complete DATE method = "; lngFinish - lngStart
    
        Months(1) = 31
        Months(2) = 29
        Months(3) = 31
        Months(4) = 30
        Months(5) = 31
        Months(6) = 30
        Months(7) = 31
        Months(8) = 31
        Months(9) = 30
        Months(10) = 31
        Months(11) = 30
        Months(12) = 31
    
        strDate1 = "01152004"
        strDate2 = "02152004"
        
        ' Record the start "time"
        lngStart = GetTickCount()
        
        ' Some process that you want to time
        For lngCounterOne = 1 To 100000
            For lngCounterTwo = 1 To 5
                lngMonth = CLng(Mid$(strDate1, 1, 2))
                lngYear = CLng(Right$(strDate1, 4))
                If lngMonth = 2 Then
                    If lngYear / 100 = lngYear \ 100 Then ' we have century
                       If lngYear / 400 = lngYear \ 400 Then ' and a leap lngYear
                           mco = 0
                       Else
                           mco = 1
                       End If
                    Else
                       If lngYear / 4 = lngYear \ 4 Then ' not on century and we have a leap lngYear
                           mco = 0
                       Else
                           mco = 1
                       End If
                    End If
                Else
                    mco = 0
                End If
                lngMonthCount = Months(lngMonth) - mco
                lngMonth = CLng(Mid$(strDate2, 1, 2))
                lngYear = CLng(Right$(strDate2, 4))
                If lngMonth = 2 Then
                    If lngYear / 100 = lngYear \ 100 Then ' we have century
                       If lngYear / 400 = lngYear \ 400 Then ' and a leap lngYear
                           mco = 0
                       Else
                           mco = 1
                       End If
                    Else
                       If lngYear / 4 = lngYear \ 4 Then ' not on century and we have a leap lngYear
                           mco = 0
                       Else
                           mco = 1
                       End If
                    End If
                Else
                    mco = 0
                End If
                lngMonthCount = Months(lngMonth) - mco
            Next lngCounterTwo
        Next lngCounterOne
        
        ' Record the finish "time"
        
        lngFinish = GetTickCount()
        
        ' Display the difference
        
        Debug.Print "Time to complete Logic/Math method = "; lngFinish - lngStart
    
    End Sub
    My goal was to compare finding the number of days in a month for two dates - a January date - which the leap year means nothing about and a Feburary date - which the leap year logic does involve.

    The immediate window showed that the method I posted (logic and math without date functions) ran in nearly half the time.

    Hopefully I didn't make some silly blunder in comparing these two - it's been a long day...

    Code:
    Time to complete DATE method =  4562 
    Time to complete Logic/Math method =  2625

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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

    Re: Days

    Well, if I had the routine, I would have posted it, but I wouldn't have tried to write it to provide as an example. It would have had to be checked for errors, as I'm sure your code was. If there were half a million instances, then maybe it would worth be using, but for my use (which is none) the date method is fine.

  23. #23
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: Days

    All that is neccessary to determine days in a month???
    Surely the DateSerial method is sufficient???

    szalamoy, err, slaminonimiy, err, ehhh,...
    salami, aren't you complicating things a bit much???

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

    Re: Days

    its too slow, apparently.

  25. #25

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

    Re: Days

    seems like everytime I post a quick-and-dirty method that solves the problem, it always gets improved upon, and quite often over-complicated. i've grown used to it.
    it was resolved last night.
    Last edited by dglienna; Mar 27th, 2005 at 09:29 PM.

  27. #27
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Days

    Quote Originally Posted by dglienna
    seems like everytime I post a quick-and-dirty method that solves the problem, it always gets improved upone, and quite often complicated. i've grown used to it.
    it was resolved last night.
    That's life we never stop learning
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  28. #28
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    DGlienna - look at this link and you will see some of my motivation for even making the point.

    This was a very long debate about the benefits of knowing ASM...

    The post and the whole thread

    If every month had a fixed number of days, then I'm guessing (and that's a really tough statement to make on this forum) that the array method of storing the number of days in a month would be chosen by nearly 100% of those polled.

    If that's true, then it's only because Feb has a 28 or 29 situation that makes us think up these neat, novel ways of picking the days in a month.

    I use the method you posted in T-SQL all the time - it's the only way, in my opinion, to arrive at the answer.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  29. #29
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    Quote Originally Posted by NotLKH
    All that is neccessary to determine days in a month???
    Surely the DateSerial method is sufficient???

    szalamoy, err, slaminonimiy, err, ehhh,...
    salami, aren't you complicating things a bit much???
    No complication at all - simple straight logic to solve a problem. The problem, in this case, in my opinion, is to determine the number of days in February. The number of days in the other 11 months is a no-brainer (not a problem - not an issue - idiom use - sorry!).

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  30. #30
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    Quote Originally Posted by dglienna
    I'd like to see the speed comparison, but as I have said before, I prefer easy to read over code that is cryptic, but saves a few cycles. VB isn't known for it's speed anyways. You can speed it up somewhat, and that's what you are doing.
    We have a strong desire in our shop to remain VB only. We have some huge algorithm and routines - report writer engines, student-class-scheduling routines - all written in VB. We have been a BASIC shop for 25+ years, because it's an easy language to maintain, support and enhance.

    But since we choose to do some "extensive" and "expensive" things in BASIC, we always consider best-choice methods.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  31. #31
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Days

    Quote Originally Posted by szlamany
    In general, when a math function can do something locally we do that.

    In my opinion, it's better to have a CASE statement with the month number and the number of days in that month.

    It's extremely easy to determine the LEAP YEAR and see if FEB has 28 or 29 days.

    Calling a series of date functions with a neat little trick of add a month, subtract a day works - but the overhead is too much for me to handle.
    Quote Originally Posted by szlamany
    An array like this:

    Code:
                Months(1) = 31
                Months(2) = 29
                Months(3) = 31
                Months(4) = 30
                Months(5) = 31
                Months(6) = 30
                Months(7) = 31
                Months(8) = 31
                Months(9) = 30
                Months(10) = 31
                Months(11) = 30
                Months(12) = 31
    ...later on in code, we take the value of the MONTH characters in the date string...

    Code:
                If month < 1 Or month > 12 Then
                    errorFlag = "This is not a valid month (enter dates as MMDDYYYY)"
                    Exit Function
                End If
                If month = 2 Then
                    If year / 100 = year \ 100 Then ' we have century
                       If year / 400 = year \ 400 Then ' and a leap year
                           MCO = 0
                       Else
                           MCO = 1
                       End If
                    Else
                       If year / 4 = year \ 4 Then ' not on century and we have a leap year
                           MCO = 0
                       Else
                           MCO = 1
                       End If
                    End If
                Else
                    MCO = 0
                End If
    
                If day < 1 Or day > Months(month) - MCO Then
                    errorFlag = "This is not a valid day (enter dates as MMDDYYYY)"
                    Exit Function
                End If
    The reason we do all this is that this is buried deep in a "general data validation" routine in our app's. We allow users to enter without SLASHES (actually prefer it) and then put the slashes in for visual aid after validation.

    This was a lot of cut/paste from an old source code on my home PC - hopefully it's clean!

    [edit] This might look like a lot of code, but in reality it's just a couple of IF statements and compiles into very fast machine code - much different then calling DATE functions that probably call API's
    My head start spinning when I read all that ...
    DGLIENNA provided solution using a very cute "one-liner" and what you suggested need a whole book to fit in ...
    To wrap it up all I want to say is this: anyone needs days count use that "one-liner" and don't look for anything else.
    If you still want to use array then don't hard code its values - create them dynamically:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i%, arDays(11) As Integer
    3.  
    4.     For i = 0 To 11
    5.         arDays(i) = Day(DateSerial(Year(Now), Month(i + 1 & "/1/" & Year(Now)) + 1, 0))
    6.         Debug.Print Format(i + 1 & "/1/" & Year(Now), "MMMM") & " - " & arDays(i) & " days"
    7.     Next i
    8.  
    9. End Sub

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

    Re: Days

    My whole point after the fourth post in the thread.

  33. #33
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    Quote Originally Posted by RhinoBull
    My head start spinning when I read all that ...
    RB - I think you might have missed my point.

    A cute-one-liner is not always a perfect solution.

    We happen to use DGLIENNA's one-liner in T-SQL stored procedures all the time. That's a standard practice here.

    But in VB we would not do that.

    If February did not have 28 or 29 days, then using that add-a-month-subtract-a-day logic would never even be considered - would it?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  34. #34
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Days

    I haven't missed any point ... I'm sure. But do you follow yourself?
    We happen to use DGLIENNA's one-liner in T-SQL stored procedures all the time. That's a standard practice here.

    But in VB we would not do that.
    ... no comments ...

    If February did not have 28 or 29 days, ...
    that's the whole point of it ...

    Why in a world do you need all those million lines of code when "one-liner" does a pretty good job ? Can you explain that? Maybe in your shop it's a "good practice" - not in my ...

    Regards.

  35. #35
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    Quote Originally Posted by RhinoBull
    Why in a world do you need all those million lines of code when "one-liner" does a pretty good job ? Can you explain that? Maybe in your shop it's a "good practice" - not in my ...

    Regards.
    Because in VB that's not a million lines of code. Actually it's very few lines of code.

    In VB the method I showed was less "lines-of-machine-code" than was the one-liner with date calls that was actually slower and more lines of machine code.

    That is an issue for us.

    Calling a language function - actually several - should be challenged.

    That is the point.

    If you saw that point already, then we simply disagree.
    Last edited by szlamany; Mar 27th, 2005 at 10:44 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  36. #36
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    Quote Originally Posted by RhinoBull
    I haven't missed any point ... I'm sure. But do you follow yourself?

    Quote:
    We happen to use DGLIENNA's one-liner in T-SQL stored procedures all the time. That's a standard practice here.

    But in VB we would not do that.


    ... no comments ...
    The difference is that we know the output of VB code and the executable logic that follows.

    In T-SQL we are simply guessing - we could develop a USER-DEFINED function in T-SQL to do the same divide by 400/100/4 and see if Feb has 28 or 29 days - but we simply do not have the experience to know that would be faster.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  37. #37
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Days

    The problem is that I think you're mixing concepts. VB is known as a HIGH LEVEL programming language. If I were worried about the speed, lenght of the executable file or how it manages its memory... I would not go with a HIGH LEVEL programming language. The idea of using a HIGH LEVEL programming language is to easy the reading and coding for the programmer. It's the computer's job to do all the interpretations needed to "downsize" to a level "she" understands.

    In other words, if I really needed speed I would code it in C or just Assembler. I don't think a load of Ifs "could" be substantiated just to make it a little faster. Processors, nowadays, are really fast and getting faster by month.

    So, I don't think a one-liner code should be replaced (as in this example or any other similar) with a 20-line code. It's faster... I agree. It's smaller... I agree. But... who cares?? I know I don't.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  38. #38
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Days

    Quote Originally Posted by szlamany
    ... If you saw that point already, then we simply disagree.
    ... and very strongly I'm afraid.
    Get out of the sixties - the sooner the better.
    64 bit processors are out there available, 64 bit OS is on the way and you are talking about 1/100 of a millisecond or something. Wake up. Common ...

  39. #39
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Days

    Quote Originally Posted by Mc Brain
    The problem is that I think you're mixing concepts. VB is known as a HIGH LEVEL programming language. If I were worried about the speed, lenght of the executable file or how it manages its memory... I would not go with a HIGH LEVEL programming language. The idea of using a HIGH LEVEL programming language is to easy the reading and coding for the programmer. It's the computer's job to do all the interpretations needed to "downsize" to a level "she" understands.

    In other words, if I really needed speed I would code it in C or just Assembler. I don't think a load of Ifs "could" be substantiated just to make it a little faster. Processors, nowadays, are really fast and getting faster by month.

    So, I don't think a one-liner code should be replaced (as in this example or any other similar) with a 20-line code. It's faster... I agree. It's smaller... I agree. But... who cares?? I know I don't.
    Using the date functions to arrive at the answer is probably more "black-box" appropriate. No one needs to know what goes on inside the function.

    Personally, I don't see those IF statements as confusing or daunting - actually I see them as a very-very simple approach to determine if Feb has 28 or 29 days.

    I think it could be argued that the DATE/YEAR/MONTH logic was more confusing - and harder to modify in the future if something needed to be changed.

    But at this point - I really don't want to debate this issue any further

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  40. #40
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Days

    Quote Originally Posted by szlamany
    Personally, I don't see those IF statements as confusing or daunting - actually I see them as a very-very simple approach to determine if Feb has 28 or 29 days.
    Nobody said it was confusing... just not practical.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

Page 1 of 2 12 LastLast

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