$DST = Get-Date
$DST.IsDaylightSavingTime()
A variable, $DST
stores the result of Get-Date
. $DST
uses the IsDaylightSavingTime method
to test if the date is adjusted for daylight savings time.
Example 7: Convert the current time to UTC time
In this example, the current time is converted to UTC time. The UTC offset for the system's locale
is used to convert the time. A table in the Notes section lists the valid UFormat
format specifiers.
Get-Date -UFormat "%A %B/%d/%Y %T %Z"
$Time = Get-Date
$Time.ToUniversalTime()
Wednesday June/26/2019 10:45:26 -07
Wednesday, June 26, 2019 17:45:26
Get-Date
uses the UFormat parameter with format specifiers to display the current system date
and time. The format specifier %Z represents the UTC offset of -07.
The $Time
variable stores the current system date and time. $Time
uses the ToUniversalTime()
method to convert the time based on the computer's UTC offset.
Example 8: Create a timestamp
In this example, a format specifier creates a timestamp String object for a directory name. The
timestamp includes the date, time, and UTC offset.
$timestamp = Get-Date -Format o | ForEach-Object { $_ -replace ":", "." }
New-Item -Path C:\Test\$timestamp -Type Directory
Directory: C:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 6/27/2019 07:59 2019-06-27T07.59.24.4603750-07.00
The $timestamp
variable stores the results of a Get-Date
command. Get-Date
uses the Format
parameter with the format specifier of lowercase o
to create a timestamp String object. The
object is sent down the pipeline to ForEach-Object
. A ScriptBlock contains the $_
variable
that represents the current pipeline object. The timestamp string is delimited by colons that are
replaced by periods.
New-Item
uses the Path parameter to specify the location for a new directory. The path
includes the $timestamp
variable as the directory name. The Type parameter specifies that a
directory is created.
Example 9: Convert a Unix timestamp
This example converts a Unix time (represented by the number of seconds since 1970-01-01 0:00:00) to DateTime.
Get-Date -UnixTimeSeconds 1577836800
Wednesday, January 01, 2020 12:00:00 AM
Example 10: Return a date value interpreted as UTC
This example shows how to interpret a date value as its UTC equivalent. For the example, this
machine is set to Pacific Standard Time. By default, Get-Date
returns values for that
timezone. Use the AsUTC parameter to convert the value to the UTC equivalent time.
PS> Get-TimeZone
Id : Pacific Standard Time
DisplayName : (UTC-08:00) Pacific Time (US & Canada)
StandardName : Pacific Standard Time
DaylightName : Pacific Daylight Time
BaseUtcOffset : -08:00:00
SupportsDaylightSavingTime : True
PS> (Get-Date -Date "2020-01-01T00:00:00").Kind
Unspecified
PS> Get-Date -Date "2020-01-01T00:00:00"
Wednesday, January 1, 2020 12:00:00 AM
PS> (Get-Date -Date "2020-01-01T00:00:00" -AsUTC).Kind
PS> Get-Date -Date "2020-01-01T00:00:00" -AsUTC
Wednesday, January 1, 2020 8:00:00 AM
Parameters
-AsUTC
Converts the date value to the equivalent time in UTC.
This parameter was introduced in PowerShell 7.1.
Type:SwitchParameter
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False
-Date
Specifies a date and time. Time is optional and if not specified, returns 00:00:00.
Enter the date and time in a format that is standard for the system locale.
For example, in US English:
Get-Date -Date "6/25/2019 12:30:22"
returns Tuesday, June 25, 2019 12:30:22
Type:DateTime
Aliases:LastWriteTime
Position:0
Default value:None
Accept pipeline input:True
Accept wildcard characters:False
Specifies the day of the month that is displayed. Enter a value from 1 to 31.
If the specified value is greater than the number of days in a month, PowerShell adds the number of
days to the month. For example, Get-Date -Month 2 -Day 31
displays March 3, not February 31.
Type:Int32
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False
-DisplayHint
Determines which elements of the date and time are displayed.
The accepted values are as follows:
Date: displays only the date
Time: displays only the time
DateTime: displays the date and time
-Format
Displays the date and time in the Microsoft .NET Framework format indicated by the format specifier.
The Format parameter outputs a String object.
For a list of available .NET format specifiers, see
Custom date and time format strings.
When the Format parameter is used, Get-Date
only gets the DateTime object's properties
necessary to display the date. As a result, some of the properties and methods of DateTime
objects might not be available.
Starting in PowerShell 5.0, you can use the following additional formats as values for the
Format parameter.
FileDate. A file or path-friendly representation of the current date in local time. The format
is yyyyMMdd
(case-sensitive, using a 4-digit year, 2-digit month, and 2-digit day). For example:
20190627.
FileDateUniversal. A file or path-friendly representation of the current date in universal
time (UTC). The format is yyyyMMddZ
(case-sensitive, using a 4-digit year, 2-digit month,
2-digit day, and the letter Z
as the UTC indicator). For example: 20190627Z.
FileDateTime. A file or path-friendly representation of the current date and time in local
time, in 24-hour format. The format is yyyyMMddTHHmmssffff
(case-sensitive, using a 4-digit
year, 2-digit month, 2-digit day, the letter T
as a time separator, 2-digit hour, 2-digit
minute, 2-digit second, and 4-digit millisecond). For example: 20190627T0840107271.
FileDateTimeUniversal. A file or path-friendly representation of the current date and time in
universal time (UTC), in 24-hour format. The format is yyyyMMddTHHmmssffffZ
(case-sensitive,
using a 4-digit year, 2-digit month, 2-digit day, the letter T
as a time separator, 2-digit
hour, 2-digit minute, 2-digit second, 4-digit millisecond, and the letter Z
as the UTC
indicator). For example: 20190627T1540500718Z.
-UFormat
Displays the date and time in UNIX format. The UFormat parameter outputs a string object.
UFormat specifiers are preceded by a percent sign (%
), for example, %m
, %d
, and %Y
. The Notes
section contains a table of valid UFormat specifiers.
When the UFormat parameter is used, Get-Date
only gets the DateTime object's properties
necessary to display the date. As a result, some of the properties and methods of DateTime
objects might not be available.
Type:String
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False
-UnixTimeSeconds
Date and time represented in seconds since January 1, 1970, 0:00:00.
This parameter was introduced in PowerShell 7.1.
Type:Int64
Aliases:UnixTime
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False
-Year
Specifies the year that is displayed. Enter a value from 1 to 9999.
Type:Int32
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False
DateTime
You can pipe a DateTime object to this cmdlet.
Outputs
DateTime
By default, this cmdlet returns a DateTime object.
When a DateTime object is sent down the pipeline to a cmdlet such as Add-Content
that expects
string input, PowerShell converts the object to a String object.
The method (Get-Date).ToString()
converts a DateTime object a String object.
To display an object's properties and methods, send the object down the pipeline to Get-Member
.
For example, Get-Date | Get-Member
.
String
When you use the Format or UFormat parameters, this cmdlet returns String objects.
Notes
DateTime objects are in long-date and long-time formats for the system locale.
The valid UFormat specifiers are displayed in the following table:
Important
UFormat specifiers are changed or added in newer versions of PowerShell. For example, %F
was
added in PowerShell 6.2, so it isn't available in Windows PowerShell 5.1 or older. Keep this in
mind when using UFormat specifiers in scripts designed to be run on multiple versions of
PowerShell.
The behavior of -UFormat %s
was changed to fix problems with the behavior in Windows PowerShell.
The return value is based on UTC time.
The value is a whole number of seconds value (no fractional part).