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

Welcome Forums General PowerShell Q&A Get-Date And AddDays Automaticly change Format of date and make Error !

Viewing 25 reply threads
  • Author
    Posts
    • #216891
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      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 !

       

      Result:

      (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 ^^

    • #216897
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      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).

    • #216912
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      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.

      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 :

      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.

      • This reply was modified 9 months, 3 weeks ago by Renaud.
      • This reply was modified 9 months, 3 weeks ago by Renaud.
      • This reply was modified 9 months, 3 weeks ago by Renaud.
      • This reply was modified 9 months, 3 weeks ago by Renaud.
    • #216915
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      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. 😉

    • #216921
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      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 !

      • This reply was modified 9 months, 3 weeks ago by Renaud.
      • This reply was modified 9 months, 3 weeks ago by Renaud.
    • #216933
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      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 …. 😉

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

      This way you have complete control over the output.

    • #216942
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      OKKKKKKkkkkkk !!!!
      Oh you are such good !!

      Please give me your skills :p

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

    • #216945
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

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

      Result:

      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 ?

    • #216948
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

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

      about_Operators Format operator -f

      How-to: The -f Format operator

    • #216951
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      …. 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:

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

      Thanks in advance.

    • #216978
      Participant
      Topics: 12
      Replies: 547
      Points: 1,345
      Helping Hand
      Rank: Community Hero

      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:

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

      Can you share .\bpplscedwin ?

    • #216981
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      Hello Sam,

       

      What do you mean about sharing bpplshedwin ?

       

    • #216984
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      The command line is :

       

       

    • #216987
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP
    • #216990
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      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 :

      Calcul of StopTime:

      at the end the command must be like that :

      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.

    • #216996
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      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.
    • #217005
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      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:

    • #217023
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

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

      Result:

      Ok…..Now i’m Crazy !

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

    • #217029
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      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.

    • #217044
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      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.

      • This reply was modified 9 months, 3 weeks ago by Renaud.
    • #217059
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      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

    • #217089
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      Ok i’ ve found for the date, it’s a bad mistake !

      After looking this script :

       

      The Variable is not using correctly there is a melting pot with this variable ($Start and $DateStart and $DateStop)
      Now it’s ok :

      Result:

      But 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.
    • #217104
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      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…

    • #217110
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      OK,just for information, this is the script working good 😉

       

      Have a good evening you both 😉

    • #217116
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      No does’nt work…

    • #217119
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      Yes yes it’s work 😉

      See you 😉

Viewing 25 reply threads
  • The topic ‘Get-Date And AddDays Automaticly change Format of date and make Error !’ is closed to new replies.