Options
Powershell date format change.
Stuck on a bit Powershell syntax (again).
Need to format a date/string variable of "25/01/2011" to "20110125".
Is there an efficient way of doing this?
Need to format a date/string variable of "25/01/2011" to "20110125".
Is there an efficient way of doing this?
Comments
-
Optionsmikedisd2 Member Posts: 1,096 ■■■■■□□□□□$DateStr = '{0:yyyyMMdd}' -f $Date
Hmm, only returned the exact same string. I may have to use a regular expression. I thought PS may have had a cleverer way of doing it.
EDIT: But this did work for converting a standard Get-Date format.
Ended up doing it ways:
$Date = 25/01/2011
$str = [regex]::Replace($Date, "(\w)(\w)(/)(\w)(\w)(/)(\w)(\w)(\w)(\w)", '$7$8$9$10$4$5$1$2')
And
$date = get-date
$date
Wednesday, 2 February 2011 12:10:25 PM
$a = '{0:yyyyMMdd}' -f $date
$a
20110202
Thanks for the help -
OptionsRobertKaucher Member Posts: 4,299 ■■■■■■■■■■EDIT: But this did work for converting a standard Get-Date format.
Ended up doing it ways:
$Date = 25/01/2011
$str = [regex]::Replace($Date, "(\w)(\w)(/)(\w)(\w)(/)(\w)(\w)(\w)(\w)", '$7$8$9$10$4$5$1$2')
And
$date = get-date
$date
Wednesday, 2 February 2011 12:10:25 PM
$a = '{0:yyyyMMdd}' -f $date
$a
20110202
Thanks for the help
That's because it is expecting a datetime datatype. You can convert a dd/mm/yyyy string to a true datetime using
$newDateTime = [datetime]::ParseExact($stringDate, "d", $null)
EDIT - Check out the Community Content posted at the bottom of this page: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx