[英]Convert date and time string to datetime format
I am developing a powershell application where i am taking input date and time as string.我正在开发一个 powershell 应用程序,我将输入日期和时间作为字符串。
Need to find the difference after that.之后需要找到差异。
But when i am trying to convert the string to datetime i am getting error.但是当我尝试将字符串转换为日期时间时,出现错误。 Below is the code and the error
下面是代码和错误
$TimeFrom = "27-08-2020 15:13:32 PM"
$TimeFrom = [datetime]::parseexact($TimeFrom, 'dd-MM-yyyy HH:mm:ss tt ', $null)
$TimeTo = "27-08-2020 15:30:32 PM"
$TimeTo = [datetime]::parseexact($TimeFrom, 'dd-MM-yyyy HH:mm:ss tt ', $null)
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."使用“3”个参数调用“ParseExact”的异常:“字符串未被识别为有效的日期时间。”
Please let me know on this.请让我知道这一点。
You have an extra space after tt
and the second conversion is reusing $TimeFrom
.在
tt
之后有一个额外的空间,第二次转换是重用$TimeFrom
。
$TimeFrom = "27-08-2020 15:13:32 PM"
$TimeFrom = [datetime]::parseexact($TimeFrom, 'dd-MM-yyyy HH:mm:ss tt', $null)
$TimeTo = "27-08-2020 15:30:32 PM"
$TimeTo = [datetime]::parseexact($TimeTo, 'dd-MM-yyyy HH:mm:ss tt', $null)
$TimeFrom
$TimeTo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.