Working across time zones can be tricky, especially when Daylight Saving Time (DST) is involved. If you’re dealing with automation, scheduling, or data transformation tasks, converting between Indian Standard Time (IST) and Central Time (CST/CDT) becomes essential.
In this post, I’ll walk you through a PowerShell function that smartly handles the conversion of an IST datetime to either CST (Central Standard Time) or CDT (Central Daylight Time), depending on the date and DST rules.
The PowerShell Function
Below is a reusable PowerShell function that converts IST to CST/CDT accurately by accounting for Daylight Saving Time.
function Convert-ISTToCSTorDST {
param (
[datetime]$ISTDate # Input IST DateTime to be converted
)
# Central Time Zone Information
$centralTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")
# Check if the given ISTDate is in Daylight Saving Time (CDT) or Standard Time (CST)
$isDST = $centralTimeZone.IsDaylightSavingTime($ISTDate)
# Convert IST to UTC (since IST is UTC +5:30)
$utcDate = $ISTDate.AddHours(-5).AddMinutes(-30)
# Convert to CST or CDT
if ($isDST) {
# If in Daylight Saving Time (CDT), subtract 5 hours from UTC to get CDT
$convertedDate = $utcDate.AddHours(-5)
Write-Host "Converted Date (CDT): $convertedDate"
} else {
# If in Standard Time (CST), subtract 6 hours from UTC to get CST
$convertedDate = $utcDate.AddHours(-6)
Write-Host "Converted Date (CST): $convertedDate"
}
return $convertedDate
}
No comments:
Post a Comment