2

Let me ask how to increment date correctly in PowerShell. We'd like to get date list in format 'yyyy-mm-dd' in designated period, to pass them to other programs.

For example, when DateStart='2018-01-05' and DateEnd='2018-01-08', then the date list should be;

2018-01-05
2018-01-06
2018-01-07
2018-01-08

Currently, my script can work as long as the designated period in the same month, like above case, but can't work in different month.

#Get_DateRange.ps1

param(
  [String] $DateStart,     
  [String] $DateEnd
)

# Variables Set
$StartDT = [Datetime]::ParseExact($DateStart,"yyyy-mm-dd",$null).AddDays(-1)
$EndDT   = [Datetime]::ParseExact($DateEnd,"yyyy-mm-dd",$null)

# Return Date List during designated period 
do
 {
  $StartDT = $StartDT.AddDays(1)
  $StartDT.toString("yyyy-mm-dd")
 } While ($StartDT -lt $EndDT)

As wrong case, when DateStart='2018-01-31' and DateEnd='2018-02-05', it only returned '2018-01-31'.

I understand obviously something wrong with my script, but so far I have no idea. Any advice would be appreciated again.

Thank you.

Additional Info: We'd like to use above script in other batch like this;

FOR /F %%d IN ('%PS_EXE%  -ExecutionPolicy ByPass  -File "Get_DateRange.ps1" -DateStart %EXT_START_DT% -DateEnd %EXT_END_DT%') DO (
@ECHO %FILE_ID%,%%d,%TARGET_DB%,%TARGET_SCHEMA%,%TARGET_TABLE%,'%%d',gz,%TARGET_DB%,%TARGET_TABLE%>>%EXT_FILE_NAME%
)
Squashman
  • 13,649
  • 5
  • 27
  • 36
Sachiko
  • 808
  • 1
  • 12
  • 31

3 Answers3

4

OK looks like all that was really wrong with the yyyy-mm-dd should have been yyyy-MM-dd

function Get-Dates(){
    param(
        [String] $DateStart,     
        [String] $DateEnd
    )
    # Variables Set
    $StartDT = [Datetime]::ParseExact($DateStart,"yyyy-MM-dd",$null).AddDays(-1)
    $EndDT   = [Datetime]::ParseExact($DateEnd,"yyyy-MM-dd",$null)
    # Return Date List during designated period 
    While ($StartDT -lt $EndDT){
        $StartDT = $StartDT.AddDays(1)
        $StartDT.toString("yyyy-MM-dd")
    }

}

Get-Dates -DateStart "2018-11-29" -DateEnd "2018-12-03"

returns

2018-11-29
2018-11-30
2018-12-01
2018-12-02
2018-12-03
ArcSet
  • 6,518
  • 1
  • 20
  • 34
  • Many, many thanks, @ArcSet. Now, it can work perfectly !! Thank you so much for your prompt and kind correction. Have a wonderful day :) – Sachiko Oct 29 '18 at 05:44
2

i see that ArcSet pointed out the error - mm instead of MM.

here's another way to do it. [grin] this grabs the number of days between the two dates and gives the start date [item zero] and one date for each of the remaining days.

$DateStart = '2018-01-31'
$DateEnd = '2018-02-05'


# Variables Set
$StartDT = Get-Date -Date $DateStart
$EndDT   = Get-Date -Date $DateEnd
$DayCount = ($EndDT - $StartDT).Days

# Return Date List during designated period 
foreach ($DC_Item in 0..$DayCount)
    {
    $StartDT.AddDays($DC_Item).ToString('yyyy-MM-dd')
    }

output ...

2018-01-31
2018-02-01
2018-02-02
2018-02-03
2018-02-04
2018-02-05
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
1

This question have the batch-file tag, so here it is a pure Batch file solution that is simple:

@echo off
setlocal EnableDelayedExpansion

set "DateStart=%1" & set "DateEnd=%2"

for /F "tokens=1-6 delims=-" %%a in ("%DateStart%-%DateEnd%") do (
   set /A "Days=366*(%%d-%%a)+31*(1%%e-1%%b)+(1%%f-1%%c)+10, MM=1%%b-100"
)

set "D=%DateStart:-=%"
for /L %%# in (1,1,%Days%) do if !D! leq %DateEnd:-=% (
   echo !D:~0,4!-!D:~4,2!-!D:~6,2!
   set /A "newMM=^!(1!D:~6!-(130+(MM+MM/8)%%2-2*^!(MM-2)+^!(!D:~0,4!%%4))), lastMM=^!(1!D:~4,2!-112), newYYYY=newMM*lastMM"
   set /A "MM+=newMM*(1-lastMM*12), D+=newYYYY*(20100-1!D:~4!) + ^!newYYYY*newMM*(100*MM+10000-1!D:~4!) + 1"
)

Example:

C:\Users\Antonio>test 2018-01-31 2018-02-05
2018-01-31
2018-02-01
2018-02-02
2018-02-03
2018-02-04
2018-02-05

If you want to use these results in a Batch file, then it is much simpler that all the code be in a single Batch file. Besides, this code run faster than the PowerShell-Batch combination... ;)

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Wow, that's great, @Aacini. I never expect it can achive all in batch ... I'm really impressed this short but brilliant scripts can also handle leap year!! Thank you so much. I re-consider batch's posibilities :) – Sachiko Oct 30 '18 at 02:00
  • Mmmm... Did you knew that you may change your selected answer if you wish? **`;)`** – Aacini Oct 30 '18 at 02:03