Welcome › Forums › General PowerShell Q&A › Get-Date And AddDays Automaticly change Format of date and make Error !
- This topic has 25 replies, 3 voices, and was last updated 9 months, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
April 9, 2020 at 8:14 am #216891
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 !PowerShell123456789101112131415161718192021222324252627$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).DaysWrite-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:
PowerShell123456789101112131415161718192021222324DiffJour: 3#######################0Avant-AddDays: 01/02/2020 16:00ApresAddDays-Start: 01/02/2020 16:00#######################1Avant-AddDays: 02/01/2020 16:00:00ApresAddDays-Start: 02/01/2020 16:00:00#######################2Avant-AddDays: 01/03/2020 16:00:00ApresAddDays-Start: 01/03/2020 16:00:00#######################3Avant-AddDays: 03/03/2020 16:00:00ApresAddDays-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 ^^
-
April 9, 2020 at 8:23 am #216897
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).
-
April 9, 2020 at 9:03 am #216912
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.PowerShell1.\bpplscedwin -0 StartTimeJ StopTimeJ -1 StartTimeJ StopTimeJ -2 StartTimeJ StopTimeJ -3 StartTimeJ StopTimeJ -4 StartTimeJ StopTimeJ -5 StartTimeJ StopTimeJ -6 StartTimeJ StopTimeJThe -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 backupafter 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 :PowerShell123456789101112131415161718192021222324DiffJour: 3#######################0Avant-AddDays: 01/02/2020 16:00ApresAddDays-Start: 01/02/2020 16:00#######################1Avant-AddDays: 01/02/2020 16:00:00ApresAddDays-Start: 02/02/2020 16:00:00#######################2Avant-AddDays: 02/02/2020 16:00:00ApresAddDays-Start: 03/02/2020 16:00:00#######################3Avant-AddDays: 03/02/2020 16:00:00ApresAddDays-Start: 04/02/2020 16:00:00It’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. -
April 9, 2020 at 9:09 am #216915
PS: what is the Format for Code ? i use PRE but you seem to say i donโt use the good format.
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. ๐
-
April 9, 2020 at 9:13 am #216921
-
April 9, 2020 at 9:37 am #216933
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 …. ๐
PowerShell1(Get-Date).AddDays(5).ToString('dd/MM/yyyy HH:mm')You could go even really fancy like this if you want:
PowerShell12$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.MinuteThis way you have complete control over the output.
-
April 9, 2020 at 9:45 am #216942
OKKKKKKkkkkkk !!!!
Oh you are such good !!Please give me your skills :p
But for the second way….Heu don’t understand….
-
April 9, 2020 at 9:54 am #216945
Ok so i just have tried, buit there is another problems…
Script:PowerShell123456789101112131415161718192021222324252627$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).DaysWrite-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:
PowerShell123456789101112131415161718192021222324DiffJour: 3#######################0Avant-AddDays: 01/02/2020 16:00ApresAddDays-Start: 01/02/2020 16:00#######################1Avant-AddDays: 01/02/2020 16:00ApresAddDays-Start: 01/02/2020 16:00#######################2Avant-AddDays: 02/02/2020 16:00ApresAddDays-Start: 02/02/2020 16:00#######################3Avant-AddDays: 04/02/2020 16:00ApresAddDays-Start: 04/02/2020 16:00The 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 ?
-
April 9, 2020 at 9:59 am #216948
But for the second wayโฆ.Heu donโt understandโฆ.
-
April 9, 2020 at 10:05 am #216951
…. Am i wrong or there is something weird ?
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:
PowerShell1.\bpplscedwin -0 StartTimeJ StopTimeJ -1 StartTimeJ StopTimeJ -2 StartTimeJ StopTimeJ -3 StartTimeJ StopTimeJ -4 StartTimeJ StopTimeJ -5 StartTimeJ StopTimeJ -6 StartTimeJ StopTimeJInstead with the real values you would use on a command line.
Thanks in advance.
-
April 9, 2020 at 11:19 am #216978
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:
PowerShell1234567891011121314151617181920212223242526function 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:
PowerShell12345678910111213141516Some-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 ?
-
April 9, 2020 at 11:34 am #216981
Hello Sam,
What do you mean about sharing bpplshedwin ?
-
April 9, 2020 at 11:35 am #216984
The command line is :
PowerShell1.\bpplschedwin $PolicyName $ScheduleLabel -0 0 0 -1 0 0 -2 0 0 -3 0 0 -4 0 0 -5 0 0 -6 0 0" -
April 9, 2020 at 11:51 am #216987
-
April 9, 2020 at 12:03 pm #216990
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:00Mardi – Mercredi
04/02/2020 16:00
05/02/2020 06:00Mercredi – Jeudi
05/02/2020 16:00
06/02/2020 06:00Jeudi – Vendredi
06/02/2020 16:00
07/02/2020 06:00Calcul of StartTime :
PowerShell123$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:
PowerShell12$StopTimeJ = (New-TimeSpan -Start $Start -End $Global:Stop).TotalSeconds=> 50400 (s)at the end the command must be like that :
PowerShell1.\bpplschedwin $PolicyName $ScheduleLabel -0 0 0 -1 57600 50400 -2 57600 50400 -3 57600 50400 -4 57600 50400 -5 0 0 -6 0 0For 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. -
April 9, 2020 at 12:08 pm #216996
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 ?
-
This reply was modified 9 months, 3 weeks ago by
Renaud.
-
This reply was modified 9 months, 3 weeks ago by
-
April 9, 2020 at 12:39 pm #217005
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:PowerShell1.\bpplschedwin $PolicyName $ScheduleLabel -0 57600 50400 -1 57600 50400 -2 57600 50400 -3 57600 50400 -4 57600 50400 -5 0 0 -6 0 0 -
April 9, 2020 at 12:51 pm #217023
I have just try something more simple (for me), to see the result of the AddDays :
PowerShell123456789101112$Start = "03/02/2020 16:00"$StartTime = ""$i = 0While($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:
PowerShell1234567891011121314151617181920212223Value of i : 0StartTime - before AddDays:StartTime - After AddDays: 03/00/2020 16:00Value of i : 1StartTime - before AddDays: 03/00/2020 16:00StartTime - After AddDays: 04/00/2020 16:00Value of i : 2StartTime - before AddDays: 04/00/2020 16:00StartTime - After AddDays: 05/00/2020 16:00Value of i : 3StartTime - before AddDays: 05/00/2020 16:00StartTime - After AddDays: 06/00/2020 16:00Value of i : 4StartTime - before AddDays: 06/00/2020 16:00StartTime - After AddDays: 07/00/2020 16:00Value of i : 5StartTime - before AddDays: 07/00/2020 16:00StartTime - After AddDays: 08/00/2020 16:00Ok…..Now i’m Crazy !
The Add is pretty good ! but..the month change to 00 ??
Tell me the bug is in my head. -
April 9, 2020 at 1:02 pm #217029
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.
-
April 9, 2020 at 1:15 pm #217044
-
April 9, 2020 at 1:24 pm #217059
Iโ just put mm instead MM as you said to me in the ToString format.
“mm” stand’s for minutes
“MM” stand’s for month -
April 9, 2020 at 1:39 pm #217089
Ok i’ ve found for the date, it’s a bad mistake !
After looking this script :
PowerShell123[String]$Start = $DateStart$StartTimeJ += (New-TimeSpan -Start ($DateStart.Split(" ")[0]) -End $DateStart).TotalSeconds$StopTimeJ += (New-TimeSpan -Start $DateStart -End $DateStop).TotalSecondsThe Variable is not using correctly there is a melting pot with this variable ($Start and $DateStart and $DateStop)
Now it’s ok :PowerShell123456789101112131415161718192021222324$Start = "03/02/2020 16:00"$Stop = "04/02/2020 06:00"$StopHebdo = "07/02/2020 06:00"$StartTimeJ = @()$StopTimeJ = @()$NbJourDiff = (New-TimeSpan -start $Start -End $StopHebdo).DaysWrite-Host "DiffJour: $JourDiff"$StartTime= ""$i = 0While($i -le $NbJourDiff){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')$StopTime = (Get-Date $Stop).AddDays($i).ToString("dd/MM/yyyy HH:mm")Write-Host "StartTime - After AddDays: $StartTime"Write-Host "StopTime - After AddDays: $StopTime"Write-Host " "$StartTimeJ += (New-TimeSpan -Start ($StartTime.Split(" ")[0]) -End $StartTime).TotalSecondsWrite-Host "StartTime: $StartTimeJ"$StopTimeJ += (New-TimeSpan -Start $StartTime -End $StopTime).TotalSecondsWrite-Host "StoptTimeJ: $StopTimeJ"$i++}Result:
PowerShell123456789101112131415161718192021222324252627282930313233DiffJour: 3Value of i : 0StartTime - before AddDays:StartTime - After AddDays: 03/02/2020 16:00StopTime - After AddDays: 04/02/2020 06:00StartTime: 57600StoptTimeJ: 50400Value of i : 1StartTime - before AddDays: 03/02/2020 16:00StartTime - After AddDays: 04/02/2020 16:00StopTime - After AddDays: 05/02/2020 06:00StartTime: 57600 57600StoptTimeJ: 50400 50400Value of i : 2StartTime - before AddDays: 04/02/2020 16:00StartTime - After AddDays: 05/02/2020 16:00StopTime - After AddDays: 06/02/2020 06:00StartTime: 57600 57600 57600StoptTimeJ: 50400 50400 50400Value of i : 3StartTime - before AddDays: 05/02/2020 16:00StartTime - After AddDays: 06/02/2020 16:00StopTime - After AddDays: 07/02/2020 06:00StartTime: 57600 57600 57600 57600StoptTimeJ: 50400 50400 50400 50400But if you have advice for another way to do this command i’m listening to you ๐
Anyway Again thxย a lotย Olaf!
Sam i have note this idea for other things, so thx to you ๐
-
This reply was modified 9 months, 3 weeks ago by
Renaud.
-
This reply was modified 9 months, 3 weeks ago by
-
April 9, 2020 at 2:01 pm #217104
Its so beautifull my script but doesn’t work becayse i have no reference to day….i’m fucked.
I need to see how to do…
-
April 9, 2020 at 2:29 pm #217110
OK,just for information, this is the script working good ๐
PowerShell1234567891011121314151617181920212223242526272829303132333435363738394041424344$Start = "03/02/2020 16:00"$Stop = "04/02/2020 06:00"$StopHebdo = "07/02/2020 06:00"$StartTimeJ = @(" "," "," "," "," "," "," ")$StopTimeJ = @(" "," "," "," "," "," "," ")$NbJourDiff = (New-TimeSpan -start $Start -End $StopHebdo).DaysWrite-Host "DiffJour: $JourDiff"$StartTime= ""$i = 0While($i -le $NbJourDiff){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')$JourStartTime = (Get-Date $StartTime).DayOfWeekWrite-Host "DaysofWeek: $JourStartTime"Switch($JourStartTime){Sunday{$StartTimeJ[0] += (New-TimeSpan -Start ($StartTime.Split(" ")[0]) -End $StartTime).TotalSeconds}Monday{$StartTimeJ[1] += (New-TimeSpan -Start ($StartTime.Split(" ")[0]) -End $StartTime).TotalSeconds}Tuesday{$StartTimeJ[2] += (New-TimeSpan -Start ($StartTime.Split(" ")[0]) -End $StartTime).TotalSeconds}Wednesday{$StartTimeJ[3] += (New-TimeSpan -Start ($StartTime.Split(" ")[0]) -End $StartTime).TotalSeconds}Thursday{$StartTimeJ[4] += (New-TimeSpan -Start ($StartTime.Split(" ")[0]) -End $StartTime).TotalSeconds}Friday{$StartTimeJ[5] += (New-TimeSpan -Start ($StartTime.Split(" ")[0]) -End $StartTime).TotalSeconds}Saturday{$StartTimeJ[6] += (New-TimeSpan -Start ($StartTime.Split(" ")[0]) -End $StartTime).TotalSeconds}}$StopTime = (Get-Date $Stop).AddDays($i).ToString("dd/MM/yyyy HH:mm")$JourStopTime = (Get-Date $StopTime).DayOfWeekSwitch($JourStopTime){Sunday{$StopTimeJ[0] += (New-TimeSpan -Start $StartTime -End $StopTime).TotalSeconds}Monday{$StopTimeJ[1] += (New-TimeSpan -Start $StartTime -End $StopTime).TotalSeconds}Tuesday{$StopTimeJ[2] += (New-TimeSpan -Start $StartTime -End $StopTime).TotalSeconds}Wednesday{$StopTimeJ[3] += (New-TimeSpan -Start $StartTime -End $StopTime).TotalSeconds}Thursday{$StopTimeJ[4] += (New-TimeSpan -Start $StartTime -End $StopTime).TotalSeconds}Friday{$StopTimeJ[5] += (New-TimeSpan -Start $StartTime -End $StopTime).TotalSeconds}Saturday{$StopTimeJ[6] += (New-TimeSpan -Start $StartTime -End $StopTime).TotalSeconds}}Write-Host "StartTime - After AddDays: $StartTime"Write-Host "StopTime - After AddDays: $StopTime"Write-Host " "Write-Host "StartTimeJ: $StartTimeJ"Write-Host "StoptTimeJ: $StopTimeJ"$i++}Have a good evening you both ๐
-
April 9, 2020 at 2:34 pm #217116
No does’nt work…
-
April 9, 2020 at 2:39 pm #217119
Yes yes it’s work ๐
See you ๐
-
-
AuthorPosts
- The topic ‘Get-Date And AddDays Automaticly change Format of date and make Error !’ is closed to new replies.