Reputation: 49
I have a list of events with date time in string format '2016/2/9 02:03:01'. I use:
$d1 = [datetime]::ParseExact('2015/12/19 08:15:32', 'yyyy/MM/dd HH:mm:ss', $null)
It got FormatException error:
以 "3" 引數呼叫 "ParseExact" 時發生例外狀況: "字串未被辨認為有效的 DateTime。"
位於 J:\Qsync\WORKSPACE\SHELL\trypsl101.ps1:310 字元:5
+ $d1 = [datetime]::ParseExact('2016/2/9 02:03:01', 'yyyy/MM/dd HH:mm:ss',
$nu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
How can I parse date string with varying digit length?
Upvotes: 1
Views: 129
Reputation: 17472
why dont use get-date simply?
$d1=get-date '2015/12/19 08:15:32'
$d2=get-date '2016/2/9 02:03:01'
Upvotes: 1
Reputation: 47832
See Custom Date and Time Format Strings.
MM
is specifically a two digit month, and dd
is a two digit day (02
, 09
, respectively).
Use M
for a month that can be specified with 1 or 2 digits, and d
for the same thing with day.
$d1 = [datetime]::ParseExact('2016/2/9 02:03:01', 'yyyy/M/d HH:mm:ss', $null)
Upvotes: 6