The main problem I found using the DatePart functions in Powershell was that for the first 9days/months of the year, it returns just a single digit rather than padded with a zero (e.g 201148 rather than 20110408). I could have got round it with some extra string manipulation, but, a little bit of guidance from @theBoardz showed that you can just use the ToString() method on the get-date Cmdlet.
$dt = (get-date).ToString("yyyy" + (Get-date).ToString("mm" + (Get-date).ToString("dd"
UPDATE: can actually do this in a single line! Thanks Alan!
(get-date).ToString("yyyyMMdd")
no worries. good work!
ReplyDeleteI find get-date -Format "yyyyMMdd" more readable; but perhaps that wasn't available in 2011?
ReplyDelete