我正在尝试将字符串变量转换为日期时间格式:
[DateTime]::ParseExact($tempdate, 'dd.MM.yyyy', [CultureInfo]::InvariantCulture).ToString('yyMMdd')
$tempdate
包含一个格式为 dd.MM.yyyy
的日期,该日期是从 Excel 文件中获取的。
遗憾的是,我收到了错误消息:
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime." At line:1 char:1 + [DateTime]::ParseExact($tempdate, 'dd.MM.yyyy', [CultureInfo]::Invaria ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FormatException
当我将“干净日期”替换变量时,它可以正常工作。
[DateTime]::ParseExact('13.03.2017', 'dd.MM.yyyy', [CultureInfo]::InvariantCulture).ToString('yyMMdd')
这个变量有什么问题,或者我该如何以其他方式将其转换为日期时间格式?
$tempdate
的类型和值是什么? - Ansgar Wiechers$tempdate
的更多信息,否则无法帮助您。wOxxOm的链接问题是否有所帮助?当前类型是什么?$tempdate.GetType().Fullname
。是否有任何前导或尾随空格?"'$tempdate'"
。 - Matt