Home > Back-end >  How convert a future date to the yyyy-mm-ddT00:00:00Z format with PowerShell
How convert a future date to the yyyy-mm-ddT00:00:00Z format with PowerShell

Time:06-21

I am trying to get this to work in PowerShell with no success. I would need to convert a future date and time (let's say July 1st 2022 midnight 00:00) to the format yyyy-mm-ddT00:00:00Z

The below command:

Get-Date -Format u

outputs to 2022-06-21 13:34:20Z (at the time of writing), which is pretty close to what i need for the present time.

Is there a way to get what i need without the use of regex or replace() method and also in the future?

CodePudding user response:

The format is pretty flexible. Just specify it manually:

Get-Date -Format yyyy-MM-ddTHH:mm:ssZ

Output: 2022-06-21T03:51:17Z

For a future date, it's probably easier to create that in advance, then use it with the formatting:

$futuredate = (Get-Date).AddDays(30)
Get-Date $futuredate -Format "yyyy-MM-ddTHH:mm:ssZ"

Output: 2022-07-21T03:56:46Z

Or, if in your case you really do want exactly midnight for the day in question:

$futuredate = (Get-Date).AddDays(10).Date
Get-Date $futuredate -Format "yyyy-MM-ddTHH:mm:ssZ"

Output: 2022-07-01T00:00:00Z

  • Related