I have a CSV file with 10 columns (the last column is the date)
Currently the date is DD/MM/YYYY and I need to change this to the US format which is MM/DD/YYYY]
What’s the best way to do this? Should I slice the day month and year into a variable and then reorder it or is there an easier way?
And just because that answer seems incredibly short - there’s actually an article linked from the Get-Date help on TechNet that gives you a full list of format types you can use. Enjoy!
Thanks Will,
Sorry I should have probably been clearer - the dates in the csv file are different dates on each line and none of them are today’s date so I need to get the date already in the file and then convert, not just put today’s date
Unfortunately there isn’t an easy way to do this. Your CSV file holds the date as a string. You need to convert that to a date. If you try this
£> $sduk = ‘25/12/2014’
£> $d = [datetime]$sduk
Cannot convert value “25/12/2014” to type “System.DateTime”. Error: “String was not recognized as a valid DateTime.”
At line:1 char:1
Casting a string to a datetime assumes invariant (basically US) format, but calling [datetime]::Parse() allows you to specify a culture (as does the ToString() method on datetime objects). For example: