Have been struggling with this for 2 days so hope somebody can stop me pulling out the rest of what little hair I have left out. I’m getting a date(string) from a CSV and then trying to convert it to a datetime object. While the conversion is easy enough it always swaps the day and month numbers around. I’ve done everything, including getting the culture as well as formatting as ‘dd/MM/yyyy’ however it always comes out as ‘MM/dd/yyyy’. Yet when I do a (get-date -format “dd/MM/yyyy”) its comes out as expected. It’s just the string that has the problem.
$CultureDateTimeFormat = (Get-Culture).DateTimeFormat
$DateFormat = $CultureDateTimeFormat.ShortDatePattern
$today = (get-date -format $DateFormat)
import-csv C:\temp\terminations.csv -Header EmployeeID, Lastname, Firstname, TerminationDate | where { $_.TerminationDate -notlike "* TFR *" -and $_.EmployeeID } | sort Lastname, Firstname -Unique | foreach-object {
$Lastname = $_.Lastname
$firstname = $_.Firstname.split("")[0]
$termstring = $_.TerminationDate
$termdate = ($_.TerminationDate.split(":")[1].Trim()) -f (get-date)
write-output "$termdate vs $today"
The result will be something like: 10/27/2019 vs 05/11/2019 (using en-AU culture). Even though the string will be 27-OCT-2019.
Thanks James, unfortunately this is still the undesired output to the below:
Is 10/21/2019 00:00:00 before 6/11/2019?
False
To clarify, $termdate is imported from a CSV as “21-OCT-2019”. Whenever I convert that to datetime it changes it to month first, then day. It needs to be day then month.
You can also have it use the culture as you have previously tried. That way it will display in the configured culture of the system rather than hard-coding the format as above. See example below.
[quote quote=186709]Thanks James, unfortunately this is still the undesired output to the below:
Is 10/21/2019 00:00:00 before 6/11/2019?
False
To clarify, $termdate is imported from a CSV as “21-OCT-2019”. Whenever I convert that to datetime it changes it to month first, then day. It needs to be day then month.
[quote quote=186748]Finally got it working with the below, tho still don’t know what happened (that Date property perhaps). Hopefully never have to compare dates again!