Results 1 to 10 of 10

Thread: [RESOLVED] DateDiff not working properly

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Resolved [RESOLVED] DateDiff not working properly

    I need to calculate the number of weeks between 2 dates. This will be used in a billing system.

    If something comes in on Thursday, they must get billed for that whole week.

    Here is my code that I tried. The code gives me wrong values. Only on the 3rd week, my week count is 1.
    vb.net Code:
    1. Private Function DIFF(ByVal THEDATE As Date)
    2.         Dim i As Integer
    3.         i = DateDiff("ww", THEDATE, Now.Date, FirstDayOfWeek.Monday)
    4.         Return i
    5.     End Function

    Honestly I cant see why this isn't working properly. Probably my lack of experience. Can anyone help me here?

    The code must be able to calculate the number of weeks between
    12/15/2008 - 30/10/2010 for example
    Last edited by tgf-47; Jul 7th, 2010 at 03:44 AM.

  2. #2
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: DateDiff not working properly

    this should work

    vb Code:
    1. Private Function diff(ByVal TheDate As Date) As Integer
    2.         Return CInt(DateDiff(DateInterval.Weekday, TheDate, Now.Date, FirstDayOfWeek.Monday, FirstWeekOfYear.System))
    3.     End Function

  3. #3

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Re: DateDiff not working properly

    Your code works much better. Except now it only skips 1 week.
    Name:  datediff.jpg
Views: 2232
Size:  34.2 KB

    My logic tells me, since the beginning of the week is Monday. Setting the date to the 1st which is a Thursday from the previous week. The week count should be one, right?

    I must be overlooking something here....

  4. #4

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Re: DateDiff not working properly

    I just saw something interesting. If I set it one day back(30'th), then it counts it as a week. So vb is just ignoring the first day of week property and just counts the days... 7 days = 1 week.

    Now does anyone have a better way of making it do what i need it to do? (Week Difference)

  5. #5
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: DateDiff not working properly

    the output is correct, from 1st jul to 7th just you are still in the first week only, the week has not yet completed, that is why it returns 0. when you go to 8th july then one week will be completed

  6. #6

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Re: DateDiff not working properly

    Yes I understand that.

    I need a way to count weeks, without the day factor.

    (monday = new week)
    Do you have any idea how i can achieve this?

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: DateDiff not working properly

    Not sure why vb.net is behaving in this manner but in vb6, I do get 1 as the week difference

    Code:
    Private Sub Command1_Click()
        d1 = #7/1/2010#
        d2 = #7/7/2010#
        
        MsgBox DateDiff("ww", d1, d2, vbMonday)
    End Sub
    Alternatively what you can do is get individual weeknumber and subtract them?

    Code:
    Dim weeknumber As Integer = DatePart(DateInterval.WeekOfYear, THEDATE)
    Last edited by Siddharth Rout; Jul 7th, 2010 at 04:49 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Re: DateDiff not working properly

    I want this to be as accurate as possible as I'm working with peoples money here.

    Since we bill per week and for full weeks only, I think I should just set the arrival date to the first day of the week that it came in and set my current date to the last day of my current week.

    vb.net Code:
    1. date1 = DateAdd("d", 0 - date1.DayOfWeek, date1)

    This would be perfect, but how do i set the default first day of the week for the above code to Monday instead of Sunday?

  9. #9

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Re: DateDiff not working properly

    I got it working!

    What is happening is the "begin date" goes back to the first day of that week (Monday) and todays date, the date that the bill will be calculated to is the last day of this week (Sunday).

    vb.net Code:
    1. Private Function diff2(ByVal date1 As Date, ByVal date2 As Date) As Integer
    2.         If date1.DayOfWeek = DayOfWeek.Sunday Then
    3.             date1 = date1.AddDays(-1)
    4.         End If
    5.         If date2.DayOfWeek = DayOfWeek.Sunday Then
    6.             date2 = date2.AddDays(-1)
    7.         End If
    8.         date1 = DateAdd("d", 1 - date1.DayOfWeek, date1) '\\\\ FIRST DAY (MONDAY) OF THAT WEEK
    9.         date2 = DateAdd("d", 6 - date2.DayOfWeek, date2) '\\\\ LAST DAY (SUNDAY) OF THAT WEEK
    10.         date2 = date2.AddDays(1)
    11.         Return CInt(DateDiff(DateInterval.Weekday, date1, date2, vbMonday, FirstWeekOfYear.Jan1))
    12.     End Function
    Last edited by tgf-47; Jul 7th, 2010 at 06:03 AM.

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

    Re: [RESOLVED] DateDiff not working properly

    This computes weeks between two days. Partial weeks are treated as full weeks

    Code:
            Dim sDT As DateTime = #6/30/2010#
            Dim eDT As DateTime = #7/14/2010#
            Dim ts As TimeSpan
            Dim numWeeks As Integer
    
            Debug.WriteLine("")
            For x As Integer = 0 To 13
    
                ts = eDT - sDT.AddDays(x - 1) 'calculate days
    
                numWeeks = CInt(Math.Ceiling(ts.TotalDays / 7))
    
                Debug.Write(sDT.AddDays(x).ToShortDateString & " " & eDT.ToShortDateString)
                Debug.WriteLine(" " & numWeeks.ToString)
    
            Next
    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

Tags for this Thread

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