Results 1 to 6 of 6

Thread: [RESOLVED] Date difference in weeks and days?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    919

    Resolved [RESOLVED] Date difference in weeks and days?

    Hello Vbforums community.
    I need an expert in date functions to help me calculate the the number of weeks and days from a datepicker.
    If I input this date 23/06/2018 in the datepicker, I should get 4 weeks and and 2 days.
    Thank you in advance

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Date difference in weeks and days?

    Since there are 4 weeks and 1 day it feels as if you don't want the difference but the range.

    Code:
    Private Sub RangeWeeksAndDays( _
        ByVal BaseDate As Date, _
        ByVal TestDate As Date, _
        ByRef RangeWeeks As Long, _
        ByRef RangeDays As Long)
    
        RangeDays = DateDiff("d", TestDate, BaseDate)
        RangeDays = RangeDays + IIf(RangeDays < 0, -1, 1)
        RangeWeeks = RangeDays \ 7
        RangeDays = RangeDays Mod 7
    End Sub
    
    Private Sub Test()
        Dim NowDate As Date
        Dim TestDate As Date
        Dim RangeWeeks As Long
        Dim RangeDays As Long
    
        NowDate = #7/22/2018#
        TestDate = #6/23/2018#
        RangeWeeksAndDays NowDate, TestDate, RangeWeeks, RangeDays
        Debug.Print RangeWeeks; RangeDays
        TestDate = #7/23/2018#
        RangeWeeksAndDays NowDate, TestDate, RangeWeeks, RangeDays
        Debug.Print RangeWeeks; RangeDays
        TestDate = #7/22/2018#
        RangeWeeksAndDays NowDate, TestDate, RangeWeeks, RangeDays
        Debug.Print RangeWeeks; RangeDays
    End Sub

    Calling Test produces:

    Code:
     4  2 
     0 -2 
     0  1

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    919

    Re: Date difference in weeks and days?

    dilettante
    You a great great man
    I'm so so grateful to you.
    This is exactly what I'm looking for and this what I need for the moment..
    But my curiosity is killing me.
    I have been playing with your code for a while trying to add number of months but I didn't succeed.
    I mean Months - weeks and days.
    Thank you very much

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    919

    Re: Date difference in weeks and days?

    duplicated message

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Date difference in weeks and days?

    If you can assume a fixed month size (such as 30 days) there isn't much more to it. This example reverses signs from what the earlier code snippet does, but it fits "reality" better:

    Code:
    Private Sub RangeMonthsWeeksAndDays( _
        ByVal StartDate As Date, _
        ByVal EndDate As Date, _
        ByRef RangeMonths As Long, _
        ByRef RangeWeeks As Long, _
        ByRef RangeDays As Long)
    
        RangeDays = DateDiff("d", StartDate, EndDate)
        'Bump the range up/down by 1 to make it inclusive:
        RangeDays = RangeDays + IIf(RangeDays < 0, -1, 1)
        RangeMonths = RangeDays \ 30
        RangeDays = RangeDays Mod 30
        RangeWeeks = RangeDays \ 7
        RangeDays = RangeDays Mod 7
    End Sub
    
    Private Sub Test()
        Dim StartDate As Date
        Dim EndDate As Date
        Dim RangeMonths As Long
        Dim RangeWeeks As Long
        Dim RangeDays As Long
    
        StartDate = #7/22/2018#
        EndDate = #6/23/2018#
        RangeMonthsWeeksAndDays StartDate, EndDate, RangeMonths, RangeWeeks, RangeDays
        Debug.Print RangeMonths; RangeWeeks; RangeDays
    
        StartDate = #7/22/2018#
        EndDate = #6/24/2018#
        RangeMonthsWeeksAndDays StartDate, EndDate, RangeMonths, RangeWeeks, RangeDays
        Debug.Print RangeMonths; RangeWeeks; RangeDays
    
        StartDate = #7/22/2018#
        EndDate = #7/29/2018#
        RangeMonthsWeeksAndDays StartDate, EndDate, RangeMonths, RangeWeeks, RangeDays
        Debug.Print RangeMonths; RangeWeeks; RangeDays
    End Sub
    Code:
    -1  0  0 
     0 -4 -1 
     0  1  1
    You can flip the signs back the other way by reversing the input dates in your calls.


    If you really want to deal with calendar months which vary in length things get more complicated of course.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    919

    Re: Date difference in weeks and days?

    Today, 03:09 AM #5
    dilettante Million thanks
    You are so helpful

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