Updated March 27, 2023
Introduction to PowerShell Convert String to Date
In many cases, it may be required to convert a string to a date variable or a date-time object. In PowerShell, this can be done in many ways. Some of the ways in which this can be done by using type casting, getting the date in US format, using parsing of Date Time. This conversion is required for many purposes like sending a string object to DB table, so a conversion is required when the input from the user is received in a string format and then needs to be formatted for further process. This article will cover in detail the various ways in which a string object can be converted to a date-time object.
Syntax:
Below is the syntax of the Datetime Parse:
[Datetime]::ParseExact('07/15/2019', 'MM/dd/yyyy', $null)
Where the first parameter is the string that needs to be converted, the second parameter is the format of the date to be produced and the third is a null character.
Code:
Write-Host "Welcome to string to date conversion example"
$string='11/11/2019'
Write-Host "The variable type now is" $string.GetType()
$string=[Datetime]::ParseExact($string, 'MM/dd/yyyy', $null)
Write-Host "After conversion the type is" $string.GetType() "and the value is" $string
Output:
Types of Date Format Available in PowerShell
Before learning about the various ways in which string can be converted to a data object, it is important to learn about various date types that are available in PowerShell. It will be helpful during conversion.
The following are the various date formats available in PowerShell.
d: Denotes ShortDate
D: Denotes LongDate
f: Denotes long date, short time
F: Denotes long date, long time
t: Denotes Short time format
T: Denotes Long time format
m, M: Month Day format
g: Denotes general data time short format
G: Denotes general data time long format
Y or y: Denotes Year month pattern
Examples of PowerShell Convert String to Date
Below are the examples of PowerShell:
Example #1
Code:
Write-Host "Date in long date format" -ForegroundColor DarkYellow
Get-Date -Format D
Write-Host "long date short time" -ForegroundColor DarkYellow
Get-Date -Format f
Write-Host "long date long time" -ForegroundColor DarkYellow
Get-Date -Format F
Write-Host "Date in general date time short format" -ForegroundColor DarkYellow
Get-Date -Format g
Write-Host "Date in general date time long format" -ForegroundColor DarkYellow
Get-Date -Format G
Write-Host "month day format" -ForegroundColor DarkYellow
Get-Date -Format m
Write-Host "Date in year format" -ForegroundColor DarkYellow
Get-Date -Format y
Output:
Example #2
Code:
Write-Host "String to Date conversion examples"
Write-Host "example of yymmddhhmm format"
[System.DateTime]::ParseExact('1605221412','yyMMddHHmm',$null)
Write-Host "Global time example"
[System.DateTime]::ParseExact('1805221412','yyMMddHHmm',[System.Globalization.DateTimeFormatInfo]::CurrentInfo)
Write-Host "US Timings example"
[System.DateTime]::ParseExact('1804221610','yyMMddHHmm',[System.Globalization.CultureInfo]::GetCultureInfo('en-US'))
Write-Host "Complex example of custome pattern"
[string]$test = 'year 2020 and date 12 and month feb and time 9 00'
[string]$testpattern = '\year yyyy an\d \da\te dd an\d \mon\t\h MMM an\d \ti\me H mm'
[DateTime]::ParseExact($test, $testpattern, $null)
Write-Host "mmddyy format example"
[System.DateTime]::ParseExact('111199','mmddyy',$null)
Write-Host "mmddyyyy format example"
[System.DateTime]::ParseExact('11111999','mmddyyyy',$null)
Output:
Example #3
Code:
Write-Host "Demo about coverting strings into dates and finding difference" -ForegroundColor DarkYellow
$stringa="01/01/2020"
$stringb="01/01/1990"
Write-Host "The current data type of the objects is" $stringa.GetType() "and" $stringb.GetType() -ForegroundColor DarkYellow
Write-Host "Converting the first string to date" -ForegroundColor DarkYellow
$date1=[System.DateTime]::ParseExact($stringa,'mm/dd/yyyy',$null)
Write-Host "the value of a the first date after conversion is "$date1 "and the type is" $date1.GetType() -ForegroundColor DarkYellow
Write-Host "Converting the second string to date" -ForegroundColor DarkYellow
$date2=[System.DateTime]::ParseExact($stringb,'mm/dd/yyyy',$null)
Write-Host "the value of a the first date after conversion is "$date2"and the type is" $date2.GetType() -ForegroundColor DarkYellow
$difference= $date2- $dateB
Write-Host "number of days difference between the two dates is " $difference.Days -ForegroundColor DarkYellow
Write-Host "number of hours difference between the two dates is " $difference.Hours -ForegroundColor DarkYellow
Write-Host "number of minutes difference between the two dates is " $difference.Minutes -ForegroundColor DarkYellow
Write-Host "number of seconds difference between the two dates is " $difference.Seconds -ForegroundColor DarkYellow
Write-Host "number of milliseconds difference between the two dates is " $difference.Milliseconds -ForegroundColor DarkYellow
Write-Host "number of total days difference between the two dates is " $difference.TotalDays -ForegroundColor DarkYellow
Write-Host "number of total hours difference between the two dates is " $difference.TotalHours -ForegroundColor DarkYellow
Write-Host "number of total minutes difference between the two dates is " $difference.TotalMinutes -ForegroundColor DarkYellow
Write-Host "number of total seconds difference between the two dates is " $difference.TotalSeconds -ForegroundColor DarkYellow
Write-Host "number of total milliseconds difference between the two dates is " $difference.TotalMilliseconds -ForegroundColor DarkYellow
Output:
Important thing to be considered while working with date
It is important to know the system’s local setting before typecasting a string into a date. If the system follows the UK date format and if we specify US time format, then it is possible that either an error may be generated, or the wrong day is displayed. For e.g., 7/16/2020 denotes July 16 in us format, whereas in the UK it generates an error as the second is considered as a month and month cannot be greater than 12. To overcome this, local cultural settings can be used.
Example #4
Code:
Write-Host "Local settings demo"
$string="13/2/2020"
$date= [DateTime] $string
Output:
In the above output, an error is thrown as the system follows as the US time zone and month come first, so it can’t be greater than 12. If we change the 13 to 12 then the following output is produced.
Code:
Write-Host "Local settings demo"
$string="12/2/2020"
$date= [DateTime] $string
Write-Host $date -ForegroundColor DarkYellow
Output:
Example #5
Code:
$tdate="06/16/2016 3:14:03 PM"
$CTDC = (Get-Culture).DateTimeFormat
Write-Host "Systems culture data time format is" $CTDC -ForegroundColor DarkYellow
$ctdcDateFormat = $CTDC.ShortDatePattern
Write-Host "systems short date pattern is" $ctdcDateFormat -ForegroundColor DarkYellow
$ctdcTimeFormat = $CTDC.LongTimePattern
Write-Host "systems long time pattern is" $ctdcTimeFormat -ForegroundColor DarkYellow
$DateTimeFormat = "$ctdcDateFormat $ctdcTimeFormat"
$DateTime = [DateTime]::ParseExact($tdate,$DateTimeFormat,[System.Globalization.DateTimeFormatInfo]::InvariantInfo,[System.Globalization.DateTimeStyles]::None)
Output:
Conclusion
Thus, the article covered in detail about the conversion from a string object to a date-time object. It explained in detail the various ways of converting a string to a date-time object, how a string object can be converted to a specific date-time format. It also explained in detail the error that may be encountered during conversion and how to overcome them. It is also wise to learn about the current system’s format before conversion. To learn more in detail it is advisable to write sample programs and practice them.
Recommended Articles
This is a guide to PowerShell Convert String to Date. Here we discuss a brief overview on PowerShell Convert String to Date and its different types along with examples. You can also go through our other suggested articles to learn more –