Welcome › Forums › General PowerShell Q&A › Convert string date format to DateTime object
- This topic has 4 replies, 4 voices, and was last updated 4 months ago by
Participant.
Viewing 4 reply threads
-
AuthorPosts
-
-
September 13, 2020 at 8:05 am #256229
How to convert time from string with format ‘Sunday, September 13, 2020 8:49:32 AM CDT‘ to DateTime object.
-
September 13, 2020 at 10:59 am #256247PowerShell1Get-Date -Date 'September 13, 2020 8:49:32 AM'
-
September 16, 2020 at 5:46 am #256961
But the time zone in string is in CDT domain, if get-date command is used time will be my system timezone.
Would it be possible to get DateTime for ‘Sunday, September 13, 2020 8:49:32 AM CDT‘ (This is in CDT timezone) and get it converted to system timezone.
-
This reply was modified 4 months ago by
mach85.
-
This reply was modified 4 months ago by
-
September 16, 2020 at 10:15 am #257012
You cannot capture time zone in this format (CDT, PDT, BST, etc.). You have to use an offset instead or do a time conversion with it stripped from the datetime string.
PowerShell1234$SourceTimeZone = [TimeZoneInfo]::FindSystemTimeZoneById('Central Standard Time')$DestinationTimeZone = [TimeZoneInfo]::Local$DateString = 'Sunday, September 13, 2020 8:49:32 AM CDT'[TimeZoneInfo]::ConvertTime(($DateString -replace '\s+CDT'),$SourceTimeZone,$DestinationTimeZone) -
September 16, 2020 at 10:26 am #257018
Here is one method:
PowerShell1[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'Central Standard Time')Timezones can be enumerated like so:
PowerShell12345foreach ($timezone in ([timezoneinfo]::GetSystemTimeZones() | select displayname, id)) {'Time in zone {0} is {1} with a local time of {2}' -f $timezone.Id,[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), $timezone.id),(Get-Date)}Output:
PowerShell123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140Time in zone Dateline Standard Time is 9/16/2020 2:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone UTC-11 is 9/16/2020 3:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Aleutian Standard Time is 9/16/2020 5:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Hawaiian Standard Time is 9/16/2020 4:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Marquesas Standard Time is 9/16/2020 4:53:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Alaskan Standard Time is 9/16/2020 6:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone UTC-09 is 9/16/2020 5:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Yukon Standard Time is 9/16/2020 7:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Pacific Standard Time (Mexico) is 9/16/2020 7:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone UTC-08 is 9/16/2020 6:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Pacific Standard Time is 9/16/2020 7:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone US Mountain Standard Time is 9/16/2020 7:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Mountain Standard Time (Mexico) is 9/16/2020 8:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Mountain Standard Time is 9/16/2020 8:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Central America Standard Time is 9/16/2020 8:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Central Standard Time is 9/16/2020 9:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Easter Island Standard Time is 9/16/2020 9:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Central Standard Time (Mexico) is 9/16/2020 9:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Canada Central Standard Time is 9/16/2020 8:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone SA Pacific Standard Time is 9/16/2020 9:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Eastern Standard Time (Mexico) is 9/16/2020 9:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Eastern Standard Time is 9/16/2020 10:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Haiti Standard Time is 9/16/2020 10:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Cuba Standard Time is 9/16/2020 10:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone US Eastern Standard Time is 9/16/2020 10:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Turks And Caicos Standard Time is 9/16/2020 10:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Paraguay Standard Time is 9/16/2020 10:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Atlantic Standard Time is 9/16/2020 11:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Venezuela Standard Time is 9/16/2020 10:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Central Brazilian Standard Time is 9/16/2020 10:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone SA Western Standard Time is 9/16/2020 10:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Pacific SA Standard Time is 9/16/2020 11:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Newfoundland Standard Time is 9/16/2020 11:53:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Tocantins Standard Time is 9/16/2020 11:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone E. South America Standard Time is 9/16/2020 11:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone SA Eastern Standard Time is 9/16/2020 11:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Argentina Standard Time is 9/16/2020 11:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Greenland Standard Time is 9/16/2020 12:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Montevideo Standard Time is 9/16/2020 11:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Magallanes Standard Time is 9/16/2020 11:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Saint Pierre Standard Time is 9/16/2020 12:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Bahia Standard Time is 9/16/2020 11:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone UTC-02 is 9/16/2020 12:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Mid-Atlantic Standard Time is 9/16/2020 1:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Azores Standard Time is 9/16/2020 2:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Cape Verde Standard Time is 9/16/2020 1:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone UTC is 9/16/2020 2:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone GMT Standard Time is 9/16/2020 3:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Greenwich Standard Time is 9/16/2020 2:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Sao Tome Standard Time is 9/16/2020 2:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Morocco Standard Time is 9/16/2020 3:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone W. Europe Standard Time is 9/16/2020 4:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Central Europe Standard Time is 9/16/2020 4:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Romance Standard Time is 9/16/2020 4:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Central European Standard Time is 9/16/2020 4:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone W. Central Africa Standard Time is 9/16/2020 3:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Jordan Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone GTB Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Middle East Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Egypt Standard Time is 9/16/2020 4:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone E. Europe Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Syria Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone West Bank Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone South Africa Standard Time is 9/16/2020 4:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone FLE Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Israel Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Kaliningrad Standard Time is 9/16/2020 4:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Sudan Standard Time is 9/16/2020 4:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Libya Standard Time is 9/16/2020 4:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Namibia Standard Time is 9/16/2020 4:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Arabic Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Turkey Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Arab Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Belarus Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Russian Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone E. Africa Standard Time is 9/16/2020 5:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Iran Standard Time is 9/16/2020 6:53:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Arabian Standard Time is 9/16/2020 6:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Astrakhan Standard Time is 9/16/2020 6:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Azerbaijan Standard Time is 9/16/2020 6:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Russia Time Zone 3 is 9/16/2020 6:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Mauritius Standard Time is 9/16/2020 6:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Saratov Standard Time is 9/16/2020 6:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Georgian Standard Time is 9/16/2020 6:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Volgograd Standard Time is 9/16/2020 6:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Caucasus Standard Time is 9/16/2020 6:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Afghanistan Standard Time is 9/16/2020 6:53:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone West Asia Standard Time is 9/16/2020 7:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Ekaterinburg Standard Time is 9/16/2020 7:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Pakistan Standard Time is 9/16/2020 7:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Qyzylorda Standard Time is 9/16/2020 7:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone India Standard Time is 9/16/2020 7:53:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Sri Lanka Standard Time is 9/16/2020 7:53:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Nepal Standard Time is 9/16/2020 8:08:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Central Asia Standard Time is 9/16/2020 8:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Bangladesh Standard Time is 9/16/2020 8:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Omsk Standard Time is 9/16/2020 8:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Myanmar Standard Time is 9/16/2020 8:53:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone SE Asia Standard Time is 9/16/2020 9:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Altai Standard Time is 9/16/2020 9:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone W. Mongolia Standard Time is 9/16/2020 9:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone North Asia Standard Time is 9/16/2020 9:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone N. Central Asia Standard Time is 9/16/2020 9:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Tomsk Standard Time is 9/16/2020 9:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone China Standard Time is 9/16/2020 10:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone North Asia East Standard Time is 9/16/2020 10:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Singapore Standard Time is 9/16/2020 10:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone W. Australia Standard Time is 9/16/2020 10:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Taipei Standard Time is 9/16/2020 10:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Ulaanbaatar Standard Time is 9/16/2020 10:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Aus Central W. Standard Time is 9/16/2020 11:08:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Transbaikal Standard Time is 9/16/2020 11:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Tokyo Standard Time is 9/16/2020 11:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone North Korea Standard Time is 9/16/2020 11:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Korea Standard Time is 9/16/2020 11:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Yakutsk Standard Time is 9/16/2020 11:23:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone Cen. Australia Standard Time is 9/16/2020 11:53:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone AUS Central Standard Time is 9/16/2020 11:53:23 PM with a local time of 9/16/2020 10:23:23 AMTime in zone E. Australia Standard Time is 9/17/2020 12:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone AUS Eastern Standard Time is 9/17/2020 12:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone West Pacific Standard Time is 9/17/2020 12:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Tasmania Standard Time is 9/17/2020 12:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Vladivostok Standard Time is 9/17/2020 12:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Lord Howe Standard Time is 9/17/2020 12:53:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Bougainville Standard Time is 9/17/2020 1:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Russia Time Zone 10 is 9/17/2020 1:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Magadan Standard Time is 9/17/2020 1:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Norfolk Standard Time is 9/17/2020 1:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Sakhalin Standard Time is 9/17/2020 1:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Central Pacific Standard Time is 9/17/2020 1:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Russia Time Zone 11 is 9/17/2020 2:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone New Zealand Standard Time is 9/17/2020 2:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone UTC+12 is 9/17/2020 2:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Fiji Standard Time is 9/17/2020 2:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Kamchatka Standard Time is 9/17/2020 3:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Chatham Islands Standard Time is 9/17/2020 3:08:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone UTC+13 is 9/17/2020 3:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Tonga Standard Time is 9/17/2020 3:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Samoa Standard Time is 9/17/2020 3:23:23 AM with a local time of 9/16/2020 10:23:23 AMTime in zone Line Islands Standard Time is 9/17/2020 4:23:23 AM with a local time of 9/16/2020 10:23:23 AM
-
-
AuthorPosts
Viewing 4 reply threads
- The topic ‘Convert string date format to DateTime object’ is closed to new replies.