在PowerShell中将字符串转换为datetime

Convert a string to datetime in PowerShell

我正在使用PowerShell尝试将字符串转换为日期时间。应该很容易,对吧?

我正在从CSV导入中获取字符串,它的格式为Jul-16。我尝试了多种方法将其转换为所需的格式,即yyyy-MM-dd,而我目前处于以下位置。

1
2
3
$invoice = $object.'Invoice Month'
$invoice ="01-" + $invoice
$invoice = [datetime]::parseexact($invoice, 'yyyy-MM-dd', $null)

但是我得到了错误:

String was not recognized as a valid DateTime.

我错过了什么吗?


ParseExact被告知要解析的日期格式,而不是您希望获取的格式。

1
2
$invoice = '01-Jul-16'
[datetime]::parseexact($invoice, 'dd-MMM-yy', $null)

如果您随后希望输出日期字符串:

1
[datetime]::parseexact($invoice, 'dd-MMM-yy', $null).ToString('yyyy-MM-dd')

克里斯


您需要指定它已经具有的格式,以便对其进行解析:

1
$InvoiceDate = [datetime]::ParseExact($invoice,"dd-MMM-yy", $null)

现在您可以按照所需的格式输出它:

1
$InvoiceDate.ToString('yyyy-MM-dd')

1
'{0:yyyy-MM-dd}' -f $InvoiceDate

您只需将字符串转换为DateTime:

1
[DateTime]"2020-7-16"

1
[DateTime]"Jul-16"

1
$myDate = [DateTime]"Jul-16";

您可以通过执行以下操作来格式化结果DateTime变量:

1
'{0:yyyy-MM-dd}' -f [DateTime]'Jul-16'

1
([DateTime]"Jul-16").ToString('yyyy-MM-dd')

1
2
$myDate = [DateTime]"Jul-16";
'{0:yyyy-MM-dd}' -f $myDate

克里斯·丹茨(Chris Dents)的答案已经涵盖了操作人员的问题,但是看到这是Google在PowerShell format string as date上的热门搜索,我想我会举一个不同的字符串示例。

如果像我一样,您将获得像这样的时间字符串20190720170000.000000+000

要注意的重要一点是,在使用[System.Management.ManagementDateTimeConverter]时需要使用ToUniversalTime(),否则您的输入会有偏移时间。

PS码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cls
Write-Host"This example is for the 24hr clock with HH"
Write-Host"ToUniversalTime() must be used when using [System.Management.ManagementDateTimeConverter]"
$my_date_24hr_time   ="20190720170000.000000+000"
$date_format         ="yyyy-MM-dd HH:mm"
[System.Management.ManagementDateTimeConverter]::ToDateTime($my_date_24hr_time).ToUniversalTime();
[System.Management.ManagementDateTimeConverter]::ToDateTime($my_date_24hr_time).ToUniversalTime().ToSTring($date_format)
[datetime]::ParseExact($my_date_24hr_time,"yyyyMMddHHmmss.000000+000",$null).ToSTring($date_format)
Write-Host
Write-Host"-----------------------------"
Write-Host
Write-Host"This example is for the am pm clock with hh"
Write-Host"Again, ToUniversalTime() must be used when using [System.Management.ManagementDateTimeConverter]"
Write-Host
$my_date_ampm_time   ="20190720110000.000000+000"
[System.Management.ManagementDateTimeConverter]::ToDateTime($my_date_ampm_time).ToUniversalTime();
[System.Management.ManagementDateTimeConverter]::ToDateTime($my_date_ampm_time).ToUniversalTime().ToSTring($date_format)
[datetime]::ParseExact($my_date_ampm_time,"yyyyMMddhhmmss.000000+000",$null).ToSTring($date_format)

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
This example is for the 24hr clock with HH
ToUniversalTime() must be used when using [System.Management.ManagementDateTimeConverter]

20 July 2019 17:00:00
2019-07-20 17:00
2019-07-20 17:00

-----------------------------

This example is for the am pm clock with hh
Again, ToUniversalTime() must be used when using [System.Management.ManagementDateTimeConverter]

20 July 2019 11:00:00
2019-07-20 11:00
2019-07-20 11:00

[Management.ManagementDateTimeConverter]上的MS文档:

https://docs.microsoft.com/zh-cn/dotnet/api/system.management.managementdatetimeconverter?view=dotnet-plat-ext-3.1


1
2
3
4
$invoice ="Jul-16"
[datetime]$newInvoice ="01-" + $invoice

$newInvoice.ToString("yyyy-MM-dd")

可以使用类型加速器,也可以使用新的变量,如果要在其他地方使用它,请像下面这样使用:$newInvoice.ToString("yyyy-MM-dd") as $newInvoice将始终为日期时间格式,除非您强制转换之后将其作为字符串,但是将失去执行日期时间功能的能力-添加天数等...


希望下面有帮助!

1
2
3
PS C:\\Users\\aameer>$invoice = $object.'Invoice Month'
$invoice ="01-" + $invoice
[datetime]$Format_date =$invoice

现在类型已转换。您可以使用method或可以访问任何属性。

1
Example :$Format_date.AddDays(5)