Get-Date And AddDays Automaticly change Format of date and make Error !

Hello all ^^,

I need to do a while to AddDays to the Initial Date like this :
The $Start or $Stop Variable need to be a string when the command line with the split are execute !

 


$Start = “01/02/2020 16:00”
$Stop = “02/02/2020 06:00”
$StopHebdo = “05/02/2020 06:00”
$JourDiff = (New-TimeSpan -start $Start -End $StopHebdo).Days
Write-Host “DiffJour: $JourDiff”
$i = 0
$StartTimeJ = @()
$StopTimeJ = @()

#Gros problème de format de date lors du New-TimeSpan !
While($i -le $jourDiff){
Write-Host “#######################”
Write-Host “$i”
Write-Host “Avant-AddDays: $Start”
#Write-Host “Avant-Stop: $Stop”
Write-Host " "
#[Datetime]$Start = $Start
[string]$DateStart = (Get-Date -Date $Start).AddDays($i)
Write-Host “ApresAddDays-Start: $Start”
[string]$DateStop = (Get-Date $Stop).AddDays($i)
#Write-Host “ApresAddDays-Stop: $Stop”
Write-Host " "
[String]$Start = $DateStart
$StartTimeJ += (New-TimeSpan -Start ($DateStart.Split(" ")[0]) -End $DateStart).TotalSeconds
$StopTimeJ += (New-TimeSpan -Start $DateStart -End $DateStop).TotalSeconds
$i++
}

Result:

DiffJour: 3
#######################
0
Avant-AddDays: 01/02/2020 16:00
 
ApresAddDays-Start: 01/02/2020 16:00
 
#######################
1
Avant-AddDays: 02/01/2020 16:00:00
 
ApresAddDays-Start: 02/01/2020 16:00:00
 
#######################
2
Avant-AddDays: 01/03/2020 16:00:00
 
ApresAddDays-Start: 01/03/2020 16:00:00
 
#######################
3
Avant-AddDays: 03/03/2020 16:00:00
 
ApresAddDays-Start: 03/03/2020 16:00:00

(Format Date is: dd/mm/YYYY HH:MM)As you see after the AddDays (Format is: mm/dd/YYYY HH:MM)
and each time the AddDays is execute the format is switch !
I have try with [Datetime] just before the AdfdDays but seem’s to be same error !

Thx for Help ^^

Could you please explain a little more detailed what you are trying to do (not the attempted solution you think you need). And if you get errors you shoud post them as well please (formatted as code as well please).

Hello OLaf ^^
How are you ?

It’s again for my NetBackup Script, i need to Fill the command line for set a Incremental Schedule of a backup Policy.
The command line is :
StartTimeJ and StopTimeJ are an array.

.\bpplscedwin -0 StartTimeJ StopTimeJ -1 StartTimeJ StopTimeJ -2 StartTimeJ StopTimeJ -3 StartTimeJ StopTimeJ -4 StartTimeJ StopTimeJ -5 StartTimeJ StopTimeJ -6 StartTimeJ StopTimeJ 

The -0 is Sunday
-1 Monday
etc…
and each couple of StartTimeJ and StopTimeJ is :

  • StartTimeJ : the Hour in second since Midnight
  • StopTimeJ is a duration in second.
    This two value is get with New-TimeSpan.

So What i want to do is to calculate the value of each StartTime end StopTime !
For this i need :

  • The Date and hour of begining
  • The date and hour of the duration
  • The date of Ending backup

after i’m using the command line Get date to now the day of begining
Use New-TimeSpan to have the time in second and difference between startTime and stopTime to have the value in second of StopTime
I need to have the numebr of days between the start and Ending to create the while.

The error i see :
The format (%d/%m/%Y %H:%M) change to (%m/%d/%Y %H:%M) and the AddDays don’t works normally, the result i wait is :

DiffJour: 3
#######################
0
Avant-AddDays: 01/02/2020 16:00
 
ApresAddDays-Start: 01/02/2020 16:00
 
#######################
1
Avant-AddDays: 01/02/2020 16:00:00
 
ApresAddDays-Start: 02/02/2020 16:00:00
 
#######################
2
Avant-AddDays: 02/02/2020 16:00:00
 
ApresAddDays-Start: 03/02/2020 16:00:00
 
#######################
3
Avant-AddDays: 03/02/2020 16:00:00
 
ApresAddDays-Start: 04/02/2020 16:00:00

It’s complicate to explain, i hope i make me understand to you.

PS: what is the Format for Code ? i use PRE but you seem to say i don’t use the good format.
I try the CODE format but nothing change.

No no, that’s jsut perfect. I meant when you post error messages you should format them the same way as you do with code, that’s all. :wink:

I have add in the precedent message : what i’m waiting as result.
We don’t see any error line, but the result is not good !

Ah … now I see what you mean. If you want to force a [DateTime] to have a specific format you just have to tell it … :wink:

(Get-Date).AddDays(5).ToString('dd/MM/yyyy HH:mm')

You could go even really fancy like this if you want:

$Date = (Get-Date).AddHours(18)
"{0:00}~{1:00}~{2:0000} {3:00}-Ξ-{4:00}" -f $Date.Day,$Date.Month,$Date.Year,$Date.Hour,$Date.Minute

This way you have complete control over the output.

OKKKKKKkkkkkk !!!
Oh you are such good !!

Please give me your skills :stuck_out_tongue:

But for the second way…Heu don’t understand…

Ok so i just have tried, buit there is another problems…
Script:

$Start = "01/02/2020 16:00"
$Stop = "02/02/2020 06:00"
$StopHebdo = "05/02/2020 06:00"
$JourDiff = (New-TimeSpan -start $Start -End $StopHebdo).Days
Write-Host "DiffJour: $JourDiff"
$i = 0
$StartTimeJ = @()
$StopTimeJ = @()

#Gros problème de format de date lors du New-TimeSpan !
While($i -le $jourDiff){
    Write-Host "#######################"
    Write-Host "$i"
    Write-Host "Avant-AddDays: $Start"
    #Write-Host "Avant-Stop: $Stop"
    Write-Host " " 
    #[Datetime]$Start = $Start
    $DateStart = (Get-Date  -Date $Start).AddDays($i).ToString("dd/MM/yyyy HH:mm") 
    Write-Host "ApresAddDays-Start: $Start"
    [string]$DateStop = (Get-Date $Stop).AddDays($i)
    #Write-Host "ApresAddDays-Stop: $Stop"
    Write-Host " " 
    [String]$Start = $DateStart
    $StartTimeJ += (New-TimeSpan -Start ($DateStart.Split(" ")[0]) -End $DateStart).TotalSeconds 
    $StopTimeJ += (New-TimeSpan -Start $DateStart -End $DateStop).TotalSeconds
    $i++
}

Result:

DiffJour: 3
#######################
0
Avant-AddDays: 01/02/2020 16:00
 
ApresAddDays-Start: 01/02/2020 16:00
 
#######################
1
Avant-AddDays: 01/02/2020 16:00
 
ApresAddDays-Start: 01/02/2020 16:00
 
#######################
2
Avant-AddDays: 02/02/2020 16:00
 
ApresAddDays-Start: 02/02/2020 16:00
 
#######################
3
Avant-AddDays: 04/02/2020 16:00
 
ApresAddDays-Start: 04/02/2020 16:00

The Days 0:

  • Nothing change, normal.
    The Days 1:
  • Before the command line , same, but after command line same ?! normally the result would be : 02/02/2020 ! No ?
    The Days 2:
  • One Days have been add, normally it would be 2 Days.

The 3 Day:

  • The date change of 2 days in a time??

Am i wrong or there is something weird ?

about_Operators Format operator -f

How-to: The -f Format operator

Sorry, I still did not get what you actually trying to calculate. Could you please provide a complete example command line like you would expect it? So not with the variables like this:

.\bpplscedwin -0 StartTimeJ StopTimeJ -1 StartTimeJ StopTimeJ -2 StartTimeJ StopTimeJ -3 StartTimeJ StopTimeJ -4 StartTimeJ StopTimeJ -5 StartTimeJ StopTimeJ -6 StartTimeJ StopTimeJ 

Instead with the real values you would use on a command line.

Thanks in advance.

Going back to the script .\bpplscedwin, I would update it to accept the backup schedule as an array of Hash Tables, one Hash Table for each of the 7 days of the week as in:

function Some-function {

    [CmdletBinding(ConfirmImpact='Low')] 
    Param(
        [Parameter(Mandatory=$false)][HashTable[]]$BackupSchedule = @(
            @{ DayOfWeek = 'Sunday';    StartTime = '16:00'; EndTime = '6:00' },
            @{ DayOfWeek = 'Monday';    StartTime = '16:00'; EndTime = '6:00' },
            @{ DayOfWeek = 'Tuesday';   StartTime = '16:00'; EndTime = '6:00' },
            @{ DayOfWeek = 'Wednesday'; StartTime = '16:00'; EndTime = '6:00' },
            @{ DayOfWeek = 'Thursday';  StartTime = '16:00'; EndTime = '6:00' },
            @{ DayOfWeek = 'Friday';    StartTime = '16:00'; EndTime = '6:00' },
            @{ DayOfWeek = 'Saturday';  StartTime = '16:00'; EndTime = '6:00' }
        )
    )

    Begin { }

    Process { 
        foreach ($Day in $BackupSchedule) {
            "Backup Schedule for '$($Day.DayOfWeek)' is '$($Day.StartTime) to $($Day.EndTime)'"
        }
    }

    End { }

}

So that when passing the input to the script/function, it’s easily understandable by other people such as:

Some-function -BackupSchedule @(
    @{ DayOfWeek = 'Sunday';    StartTime = '16:00'; EndTime = '6:00' },
    @{ DayOfWeek = 'Monday';    StartTime = '17:00'; EndTime = '6:00' },
    @{ DayOfWeek = 'Tuesday';   StartTime = '18:00'; EndTime = '6:00' },
    @{ DayOfWeek = 'Wednesday'; StartTime = '14:00'; EndTime = '6:00' },
    @{ DayOfWeek = 'Thursday';  StartTime = '15:00'; EndTime = '6:00' },
    @{ DayOfWeek = 'Friday';    StartTime = '12:00'; EndTime = '6:00' },
    @{ DayOfWeek = 'Saturday';  StartTime = '11:00'; EndTime = '6:00' }
)
Backup Schedule for 'Sunday' is '16:00 to 6:00'
Backup Schedule for 'Monday' is '17:00 to 6:00'
Backup Schedule for 'Tuesday' is '18:00 to 6:00'
Backup Schedule for 'Wednesday' is '14:00 to 6:00'
Backup Schedule for 'Thursday' is '15:00 to 6:00'
Backup Schedule for 'Friday' is '12:00 to 6:00'
Backup Schedule for 'Saturday' is '11:00 to 6:00'

Can you share .\bpplscedwin ?

Hello Sam,

 

What do you mean about sharing bpplshedwin ?

 

The command line is :

 

 


.\bpplschedwin $PolicyName $ScheduleLabel -0 0 0 -1 0 0 -2 0 0 -3 0 0 -4 0 0 -5 0 0 -6 0 0"

https://www.veritas.com/content/support/en_US/doc/15263389-133604923-0/v133173011-133604923

To reply to OLaf:

When we launche the script we ask :
Date and hour of start
Date and hour of end the next days
Date and hour of the ending of the week.

We will take a Week like:
Lundi - Mardi
03/02/2020 16:00
04/02/2020 06:00

Mardi - Mercredi
04/02/2020 16:00
05/02/2020 06:00

Mercredi - Jeudi
05/02/2020 16:00
06/02/2020 06:00

Jeudi - Vendredi
06/02/2020 16:00
07/02/2020 06:00

Calcul of StartTime :

$Jour = (Get-Date -Date $Start.split(" ")[0] -UFormat "%A").Replace(" ","")
$StartTime = (New-TimeSpan -Start ($Start.Split(" ")[0]) -End $Start).TotalSeconds
=> 57600 (s)

Calcul of StopTime:

$StopTimeJ = (New-TimeSpan -Start $Start -End $Global:Stop).TotalSeconds
=> 50400 (s)

at the end the command must be like that :

.\bpplschedwin $PolicyName $ScheduleLabel -0 0 0 -1 57600 50400 -2 57600 50400 -3 57600 50400 -4 57600 50400 -5 0 0 -6 0 0

For this command the variable $StartTime and $StopTime are an Array.
And we calculate the number of day between the start and the end of week.

Sam, the problem i see with your way, is we must fill the Hour of StartTime and StopTime each days.
I need this will be calculate by the script not by the user ending.

What do you think about this Sam ?

But what for would you need the actual date. bpplschedwin takes only the day of the week anyway. There’s no representation for the 9th of April 2020 - it’s Thursday. So it’s “-4”.
So a complete work week with the start at 16:00 and the end at 06:00 the next day would be this:

.\bpplschedwin $PolicyName $ScheduleLabel -0 57600 50400 -1 57600 50400 -2 57600 50400 -3 57600 50400 -4 57600 50400 -5 0 0 -6 0 0

I have just try something more simple (for me), to see the result of the AddDays :

$Start = "03/02/2020 16:00"
$StartTime = ""
$i = 0
While($i -le 5){
    Write-Host ""
    Write-Host "Value of i : $i"
    Write-Host "StartTime - before AddDays: $StartTime"
    $StartTime = (Get-Date $Start).AddDays($i).ToString("dd/mm/yyyy HH:mm")
    Write-Host "StartTime - After AddDays: $StartTime"
    #$StartTime = (New-TimeSpan -Start $Start -End)
    $i++
}

Result:

Value of i : 0
StartTime - before AddDays: 
StartTime - After AddDays: 03/00/2020 16:00

Value of i : 1
StartTime - before AddDays: 03/00/2020 16:00
StartTime - After AddDays: 04/00/2020 16:00

Value of i : 2
StartTime - before AddDays: 04/00/2020 16:00
StartTime - After AddDays: 05/00/2020 16:00

Value of i : 3
StartTime - before AddDays: 05/00/2020 16:00
StartTime - After AddDays: 06/00/2020 16:00

Value of i : 4
StartTime - before AddDays: 06/00/2020 16:00
StartTime - After AddDays: 07/00/2020 16:00

Value of i : 5
StartTime - before AddDays: 07/00/2020 16:00
StartTime - After AddDays: 08/00/2020 16:00

Ok…Now i’m Crazy !

The Add is pretty good ! but…the month change to 00 ??
Tell me the bug is in my head.

Oh i don’t see you have reply.

I need to actual date because, for example when we have a maintenance the week End and we could not done a full backup, we want to make possible to do an Incremental during this maintenance Week-End.

Ok i’v found the reason of the error, it’s because the format in the ToString, is not good.

I’ just put mm instead MM as you said to me in the ToString format.