Overview
[DateTime]
Timestamp as date and time
Date
, Day
, DayOfWeek
, DayOfYear
, Hour
, Kind
, Millisecond
, Minute
, Month
, Second
, Ticks
, TimeOfDay
, Year
, DateTime
[DateTimeOffset]
Time interval contains
Date
, DateTime
, Day
, DayOfWeek
, DayOfYear
, Hour
, LocalDateTime
, Millisecond
, Minute
, Month
, Offset
, Second
, Ticks
, TimeOfDay
, UtcDateTime
, UtcTicks
, Year
Get current timestamp
[ DateTime ] $currentTimestamp1 = Get-Date
[ DateTime ] $currentTimestamp2 = [ DateTime ]::Now
[ DateTime ] $currentUtcTimestamp = [ DateTime ]::UtcNow
[ Int64 ] $unixMillis = [ DateTimeOffset ]::Now.ToUnixTimeMilliseconds()
[ Int64 ] $unixSeconds = [ DateTimeOffset ]::Now.ToUnixTimeSeconds()
Get specific timestamp
[ DateTime ] $nowNewYearsEve = Get-Date - Year 1999 - Month 12 - Day 31
[ DateTime ] $midnightNewYearsEve = [ DateTime ] "1999-12-31"
[ DateTime ] $todayQuarterToTwelve = [ DateTime ] "23:45:33"
[ DateTime ] $13ofJanuary2007 = [ DateTime ] "1.13.07 12:30"
Get relative timestamp
[ DateTime ] $lastMidnight = [ DateTime ]::Now - [ DateTime ]::Now.TimeOfDay
[ DateTime ] $yesterday = [ DateTime ]::Now.AddDays( -1 )
[ DateTime ] $firstThisMonth = Get-Date - Date $lastMidnight - Day 1
Import Unix Milliseconds timestamp
[ DateTime ] $importUnixMillis = 1673978762521 | foreach {
[ DateTimeOffset ]::FromUnixTimeSeconds( $_ ).DateTime
}
Import Unix Seconds timestamp
[ DateTime ] $importUnixSeconds = 1674064339 | foreach {
[ DateTimeOffset ]::FromUnixTimeSeconds( $_ ).DateTime
}
Import timestamp using current culture
[ DateTime ] $date = "13.04.2022" | foreach {
[ DateTime ]::Parse( $_ , [ CultureInfo ]::CurrentCulture)
}
Import timestamp using a specific culture
$germanCulture = [ cultureinfo ]::GetCultureInfo( 'de-DE' )
[ DateTime ] $date = "13.04.2022" | foreach {
[ DateTime ]::Parse( $_ , $germanCulture)
}
Import timestamp using a specific format
$dateFormat = "yyyyMMdd"
[ DateTime ] $date = "20220413" | foreach {
[ DateTime ]::ParseExact( $_ , $dateFormat , [ CultureInfo ]::InvariantCulture)
}
Sources:
Related:
Tags:
PowerShell Objects - Handle, Import, Export, Filter and RegEx query objects