PowerShell Tips: Date and Time Usage in Scripts

PowerShell: Get-Date Tips

We have seen and experienced many ways of using PowerShell Get-Date and [DateTime]. Comparing to Get-Date, [Datetime] is faster by a few milliseconds. However, both do the same job. I always prefer to use [DateTime] rather than Get-Date. DateTime is a ScriptProperty of Get-Date.

Get-Date | GM

Reference Screen Shots: Using Date and Time - PowerShell Tips:

Date Time Formats

(Get-Date).GetDateTimeFormats().Count
(Get-Date).GetDateTimeFormats()

To Get Leap Years?

2000..2020 | %{( $_ , ':', [DateTime]::IsLeapYear($_)) -join ''}

To Add Days

(Get-Date).AddDays(10)

[DateTime]::Now.AddDays(1)

[DateTime]::Now + 8d

Days In Month

[DateTime]::DaysInMonth(2014, 6)

Week In Month

(Get-WmiObject -Class Win32_LocalTime).WeekInMonth