1
0
mirror of https://github.com/jaapbrasser/SharedScripts.git synced 2025-12-24 21:51:38 +02:00
Files
SharedScripts/Convert-APIDateTime/Convert-APIDateTime.ps1

49 lines
1.3 KiB
PowerShell
Raw Permalink Normal View History

2019-08-12 17:31:20 +02:00
function Convert-APIDateTime {
<#
2021-01-17 23:53:50 +01:00
.SYNOPSIS
Function to convert specific date time format from API endpoint to a datetime object
2019-08-12 17:31:20 +02:00
2021-01-17 23:53:50 +01:00
.DESCRIPTION
This function takes a specific date time string and converts it to a datetime object
2019-08-12 17:31:20 +02:00
2021-01-17 23:53:50 +01:00
.EXAMPLE
Convert-APIDateTime "Thu Aug 08 20:31:36 UTC 2019"
Thursday, August 8, 2019 8:31:36 PM
2019-08-12 17:31:20 +02:00
#>
2019-08-12 17:33:41 +02:00
[cmdletbinding()]
2019-08-12 17:31:20 +02:00
param(
[parameter(
Position = 0,
Mandatory = $true,
ValueFromPipeline = $true
)]
2019-08-12 17:33:41 +02:00
[ValidateNotNullOrEmpty()]
2019-08-12 17:31:20 +02:00
[string] $DateTimeString
)
begin {
[System.Globalization.DateTimeFormatInfo]::InvariantInfo.get_abbreviatedmonthnames() | ForEach-Object -Begin {
$MonthHash = @{}
$Count = 0
} -Process {
$Count++
if ($_) {
$MonthHash.$_ = $Count.ToString().Padleft(2,'0')
}
}
}
process {
2019-08-12 17:35:37 +02:00
$NewDateTimeString = $DateTimeString.Substring(4) -replace 'UTC '
2019-08-12 17:31:20 +02:00
$MonthHash.GetEnumerator() | ForEach-Object {
$NewDateTimeString = $NewDateTimeString -replace $_.Key,$_.Value
}
try {
[DateTime]::ParseExact($NewDateTimeString,'MM dd HH:mm:ss yyyy',$null)
} catch {
Write-Error $_
}
}
}