Question

I need to create a time-frame that is since 3 hours ago and before NOW by setting 2 string parameters:

[string] since
[string] before

whereas each parameter is in a DateTimeOffset format as : "yyyy-MM-dd HH:mm:ss z:00"

How can I set since and before parameters in PowerShell?

Was it helpful?

Solution

The DateTime structure contains useful methods, like .AddHours() and .ToString(). Just add -3 hours to current date. Use the standard date and time format strings to get desired output or roll a cusom one.

PS C:\> $now = get-date
PS C:\> $then = $now.AddHours(-3)
PS C:\> $now.ToString("u")
2013-06-03 18:43:29Z
PS C:\> $then.ToString("u")
2013-06-03 15:43:29Z
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top