Date and Time
Explore how to work with date and time in Python and PowerShell by understanding retrieval methods, converting strings to datetime, formatting, and time duration calculation using timedelta and TimeSpan. Gain practical skills for manipulating dates and times across both languages.
Get date and time
Let’s begin by understanding how to retrieve date and time information.
System.DateTime
Getting date and time in PowerShell is done using the cmdlet Get-Date or using the [datetime] type accelerator.
Under the hood, both methods call .net class’ System.DateTime to return rich DateTime objects. If we dig a little deeper, we’ll see that there is no difference at all in the returned objects.
[datetime].FullName
(Get-Date).GetType().FullName
If we pipe the results of the Get-Date cmdlet to Get-Member, we will find the member properties and methods of the DateTime object like Day, Hour, Minute, Second, AddDays(), and AddHours().
Get-Date | Get-Member
These properties can be utilized to get the current date and time:
Methods of class System.DateTime time objects can be used to perform various date-time operations like adding years, hours, or minutes to the current date.
## using date time methods
(Get-Date).AddYears(2) # current year + ...