Jump to content

Obtain a list of dates from _DateDiff or similar funcs


Recommended Posts

Hi everybody!

I've done an (hopefully) extensive research on the forum and on google but didn't find anything about this.

I have to obtain a list of dates from _DateDiff or other funcs.

For example, the user select 2019/02/04 from a DatePicker and 2019/01/02 from the second DatePicker, and I have to calculate all the dates between:

- 2019/02/03

- 2019/02/02

- 2019/02/01

- 2019/01/31

- 2019/01/30

[...]

and so on.

I can't figure out how to do it.

By now I managed to obtain days, month and year difference using _DateDiff, but it's a big pain to obtain single dates between the range.

 

Thank you!

Link to comment
Share on other sites

Do you mean something like:

Updated to account for -+ days

#include <Date.au3>
 Local $sDate1 = "2019/02/04", $sDate2 = "2019/01/02"
 Local $iNoOfDays = _DateDiff("D", $sDate1, $sDate2)
 If $iNoOfDays < 0 Then
    For $i = $iNoOfDays To 0
        ConsoleWrite(_DateAdd("D", $i, $sDate1) & @CRLF)
    Next
Else
    For $i = 0 To $iNoOfDays
        ConsoleWrite(_DateAdd("D", $i, $sDate1) & @CRLF)
    Next
EndIf

 

Edited by Subz
Link to comment
Share on other sites

11 minutes ago, Subz said:

Do you mean something like:

Updated to account for -+ days

#include <Date.au3>
 Local $sDate1 = "2019/02/04", $sDate2 = "2019/01/02"
 Local $iNoOfDays = _DateDiff("D", $sDate1, $sDate2)
 If $iNoOfDays < 0 Then
    For $i = $iNoOfDays To 0
        ConsoleWrite(_DateAdd("D", $i, $sDate1) & @CRLF)
    Next
Else
    For $i = 0 To $iNoOfDays
        ConsoleWrite(_DateAdd("D", $i, $sDate1) & @CRLF)
    Next
EndIf

 

Wonderful!
I didn't know _DateAdd function!

This solves my problem completely :)

Link to comment
Share on other sites

Here are two methods to get a descending order set of dates between two dates.

#include <Date.au3>
#include <Math.au3>

Local $sDate1 = "2019/01/02", $sDate2 = "2019/02/04"

; -------------------- Method 1 --------------------------------
Local $aJulDate1 = StringRegExp($sDate1, "(\d{4}).(\d{2}).(\d{2})", 3), $aJulDate2 = StringRegExp($sDate2, "(\d{4}).(\d{2}).(\d{2})", 3) ; <- Arrays
Local $iJulDate1 = _DateToDayValue($aJulDate1[0], $aJulDate1[1], $aJulDate1[2]), $iJulDate2 = _DateToDayValue($aJulDate2[0], $aJulDate2[1], $aJulDate2[2]) ; <- Day values
Local $Y, $M, $D

For $i = _Max($iJulDate1, $iJulDate2) To _Min($iJulDate1, $iJulDate2) Step -1
    _DayValueToDate($i, $Y, $M, $D)
    ConsoleWrite($Y & "/" & $M & "/" & $D & @CRLF)
Next

ConsoleWrite("==== Or ====" & @CRLF)

; -------------------- Method 2 --------------------------------
Local $iNoOfDays = _DateDiff("D", $sDate1, $sDate2)
For $i = Abs($iNoOfDays) To 0 Step -1
    ConsoleWrite(_DateAdd("D", $i, ($iNoOfDays < 0 ? $sDate2 : $sDate1)) & @CRLF)
Next

 

Link to comment
Share on other sites

1 hour ago, Malkey said:

Here are two methods to get a descending order set of dates between two dates.

#include <Date.au3>
#include <Math.au3>

Local $sDate1 = "2019/01/02", $sDate2 = "2019/02/04"

; -------------------- Method 1 --------------------------------
Local $aJulDate1 = StringRegExp($sDate1, "(\d{4}).(\d{2}).(\d{2})", 3), $aJulDate2 = StringRegExp($sDate2, "(\d{4}).(\d{2}).(\d{2})", 3) ; <- Arrays
Local $iJulDate1 = _DateToDayValue($aJulDate1[0], $aJulDate1[1], $aJulDate1[2]), $iJulDate2 = _DateToDayValue($aJulDate2[0], $aJulDate2[1], $aJulDate2[2]) ; <- Day values
Local $Y, $M, $D

For $i = _Max($iJulDate1, $iJulDate2) To _Min($iJulDate1, $iJulDate2) Step -1
    _DayValueToDate($i, $Y, $M, $D)
    ConsoleWrite($Y & "/" & $M & "/" & $D & @CRLF)
Next

ConsoleWrite("==== Or ====" & @CRLF)

; -------------------- Method 2 --------------------------------
Local $iNoOfDays = _DateDiff("D", $sDate1, $sDate2)
For $i = Abs($iNoOfDays) To 0 Step -1
    ConsoleWrite(_DateAdd("D", $i, ($iNoOfDays < 0 ? $sDate2 : $sDate1)) & @CRLF)
Next

 

while it's easier to use _DateAdd in my case, the first method is interesting...it's always interesting to see a proper regexp and to build manually the result.

 

Thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...