Last active
February 23, 2017 16:21
-
-
Save auberginehill/eb07d0c781c09ea868123bf519374ee8 to your computer and use it in GitHub Desktop.
Examples of time difference calculations (between now and an arbitrary future date) and other date and time related tasks in Windows PowerShell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Get-TimeDifference.ps1 | |
Windows PowerShell: For a more comfortable reading experience, please type | |
help ./Get-TimeDifference -Full | |
#> | |
<# | |
.SYNOPSIS | |
Examples of time difference calculations (between now and an arbitrary future date) | |
and other date and time related tasks in Windows PowerShell. | |
.DESCRIPTION | |
The script will not output anything. Please type | |
help ./Get-TimeDifference -Full | |
to see the included date and time related examples. | |
.EXAMPLE | |
(Get-Date -year 2099 -month 1 -day 1 -hour 0 -minute 0 -second 0) - (Get-Date) | |
This example calculates the time difference between the time that the command is | |
run and the start of the year 2099 in the Gregorian calendar (January 1, 2099 at | |
00:00:00 hours). The output is always the same regardles of the current locale | |
settings. | |
Days : 30086 | |
Hours : 4 | |
Minutes : 10 | |
Seconds : 36 | |
Milliseconds : 0 | |
Ticks : 25994454360000000 | |
TotalDays : 30086,1740277778 | |
TotalHours : 722068,176666667 | |
TotalMinutes : 43324090,6 | |
TotalSeconds : 2599445436 | |
TotalMilliseconds : 2599445436000 | |
Universal = $true | |
.EXAMPLE | |
New-TimeSpan -End (Get-Date -year 2099 -month 1 -day 1 -hour 0 -minute 0 -second 0) | |
In this example a new TimeSpan object is created that represents the aforementioned | |
time interval. The command does not require the -Start parameter, because the default | |
value of the -Start parameter in New-TimeSpan is the current date and time. The | |
output is always the same regardles of the current locale settings. | |
Days : 30086 | |
Hours : 4 | |
Minutes : 10 | |
Seconds : 36 | |
Milliseconds : 0 | |
Ticks : 25994454360000000 | |
TotalDays : 30086,1740277778 | |
TotalHours : 722068,176666667 | |
TotalMinutes : 43324090,6 | |
TotalSeconds : 2599445436 | |
TotalMilliseconds : 2599445436000 | |
Universal = $true | |
.EXAMPLE | |
(Get-Date) + (New-TimeSpan -days 90) | |
This command returns the date that is 90 days after the current date and displays | |
it in the default date and time format, which depends on the current locale setting, | |
so the output is not universal, but varies according to the prevalent settings. | |
Default Locale: Friday, June 16, 2006 10:31:27 AM | |
Universal = $false | |
.EXAMPLE | |
Get-Date -Format g | |
This command retrieves the current date and time and formats it in short-date | |
and short-time format according to the current locale settings. It uses the .NET | |
Framework "g" format specifier (General [short date and short time]) to | |
specify the format. | |
Locale 1: 13.6.2006 12:46 | |
Locale 2: 6/13/2006 12:46 PM | |
Universal = $false | |
.EXAMPLE | |
Get-Date -Format HH:mm:ss | |
This command retrieves a 24-hour timestamp that is in the 24-hour format regardles | |
of the current locale settings, so the output is universally always the same. | |
17:13:03 | |
Universal = $true | |
.EXAMPLE | |
(Get-Date).ToUniversalTime() | |
This command converts the current date and time to UTC time and displays it in the | |
format that is specified in the locale settings, so that for example, the name of | |
the month name is translated to the language that the computer is using. | |
Tuesday, June 13, 2006 8:09:19 PM | |
UniversalTime = $true | |
'UniversalTime (UTC)' = $true | |
'Universal Date and Time Display Format' = $false | |
'Local Time' = If ( $LocalTime -eq $UniversalTimeUTC ) { | |
$true | |
} Else { | |
$false | |
} # else | |
.EXAMPLE | |
Get-Date ((Get-Date).ToUniversalTime()) -Format HH:mm:ss | |
This command converts the current time to UTC time and displays it in the | |
24-hour format regardles of the current locale settings, so the output is | |
universally always the same. | |
16:18:46 | |
Universal = $true | |
UniversalTime = $true | |
'UniversalTime (UTC)' = $true | |
'Universal Date and Time Display Format' = $true | |
'Local Time' = If ( $LocalTime -eq $UniversalTimeUTC ) { | |
$true | |
} Else { | |
$false | |
} # else | |
.EXAMPLE | |
Get-WmiObject -Class Win32_BIOS -ComputerName $env:COMPUTERNAME | Format-List -property Name, @{Label="BIOS Age"; Expression={ [string]((Get-Date) - $_.ConvertToDateTime($_.ReleaseDate)).Days + ' Days' }} | |
Windows Management Instrumentation (WMI) uses a different date-time object than the .NET | |
Framework date-time object that Get-Date returns. To use date-time information | |
from WMI in a command with date-time information from Get-Date, ConvertToDateTime | |
method could be used to convert WMI CIM_DATETIME objects to .NET Framework DateTime | |
objects. | |
In this example the name and age of the BIOS of the computer is displayed in days. | |
The output is always the same regardles of the current locale settings. | |
Name : Default System BIOS | |
BIOS Age : 345 Days | |
Universal = $true | |
.EXAMPLE | |
(Get-Date).IsDaylightSavingTime() | |
This command tells, whether the current date and time are adjusted for daylight | |
savings time in the current locale and returns True or False accordingly. The | |
format of the output is always the same regardles of the current locale settings. | |
True / False | |
Result = If ( ($LocalDateAndTime -ge $StartOfTheDaylightSavingTimeInCurrentYear) -and ($LocalDateAndTime -lt $EndOfTheDaylightSavingTimeInCurrentYear) ) { | |
'True' | |
} Else { | |
'False' | |
} # else | |
Universal = $true | |
UniversalOutputFormat = $true | |
UniversalResults = $false | |
.LINK | |
Get-Date: http://go.microsoft.com/fwlink/?LinkID=113313 | |
New-TimeSpan: http://go.microsoft.com/fwlink/?LinkID=113360 | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Days : 30086
Hours : 4
Minutes : 10
Seconds : 36
Milliseconds : 0
Ticks : 25994454360000000
TotalDays : 30086,1740277778
TotalHours : 722068,176666667
TotalMinutes : 43324090,6
TotalSeconds : 2599445436
TotalMilliseconds : 2599445436000