Working with time and date in Powershell

PowerShell

 

Being a former web developer and working with some scripting languages during the years, i must say that Powershell makes it so easy to work with dates.

Let’s see some code and tips&tricks for time and dates in Powershell.

Get current date


Get-Date

 

This returns the date in the format: Sunday, August 4, 2019 12:01:26 AM

 

Get current date and current time in a desired format

 


$date = Get-Date -format dd.MM.yyyy
$time = Get-Date -format HH:mm:ss

write-host $date + " " + $time

 

This will return 04.08.2019 01:06:26

For more types of formats check out the official Microsoft docs page.

 

Add days or minutes to current date

 


$AddMinutes = (Get-Date).AddMinutes(7)

$AddDays = (Get-Date).AddDays(7)

$AddMinutesToDate = (Get-Date).Date.AddMinutes(7)

$AddDaysToDate  = (Get-Date).Date.AddDays(7)

 

You might wonder why i made so many variables and what is the difference between them.

Let’s say the current date for me is Sunday, August 4, 2019 12:25:57 AM

The first two variables will result as:

$AddMinutes = Sunday, August 4, 2019 12:32:57 AM

$AddDays = Sunday, August 11, 2019 12:25:57 AM

 

So far everything seem good right? So what about the other two?

The last two variables will result as:

$AddMinutesToDate = Sunday, August 4, 2019 12:07:00 AM

$AddDaysToDate = Sunday, August 11, 2019 12:00:00 AM

If you add the extra parameter .Date, this will round up the date to 12:00:00 AM each time. So be careful how you construct it.

 

Add days or minutes to date from String (convert string to date)

 

Let’s say you have a date, but as a string in your script. If you want to try, for example, to add days to a simple string:

 


$StringDate = "04.08.2019"

$AddDays = $StringDate.Date.AddDays(7)

 

You will receive and error saying that the variable $StringDate could not be converted to date.

So how do you convert a string to date in Powershell?

Well, it’s quite easy:

 


$StringDate = "04.08.2019"

$AddDays = (Get-Date $StringDate).Date.AddDays(4)

 

This will result in $AddDays = 08.08.2019

 

Compare current date with string date

 

Usually, when you want to compare dates, you most certainly have a date in a string type of variable.

If you try something like:

 


$Date1 = "05.08.2019"

$Date2 = Get-Date

If ($Date1 -gt $Date2){

write-host "Date 1 is greater than Date 2"

}

You will see that this will fail. In order to make any date comparisons, you must convert you string to date.

The correct way to do this is:

 


$Date1 = "05.08.2019"

$Date2 = Get-Date

If ((get-date $Date1) -gt (get-date $Date2)){

write-host "Date 1 is greater than Date 2"

}

 

With the Get-Date before the $Date1 variable, this converts your string to a date, thus the comparison will work.

Dates

Do you have any other Date scenarios that i missed here? Leave me a message.

Sharing is caring!

Leave a comment

Your email address will not be published. Required fields are marked *

four + 2 =