在PowerShell中将字符串转换为datetime [英] Convert a string to datetime in PowerShell
本文介绍了在PowerShell中将字符串转换为datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用PowerShell尝试将字符串转换为日期时间。
I am using PowerShell to try and convert a string to a datetime. It should be easy, right?
我从CSV导入中获取字符串,它的格式为 Jul-16
I am getting the string from a CSV import, and it comes in the format of Jul-16
. I have tried multiple ways of getting it into the format I want which is yyyy-MM-dd
and I am currently at the following.
$invoice = $object.'Invoice Month'
$invoice = "01-" + $invoice
$invoice = [datetime]::parseexact($invoice, 'yyyy-MM-dd', $null)
但是我得到了错误:
字符串未被识别为有效的DateTime。
String was not recognized as a valid DateTime.
我遗漏了一些东西吗?
推荐答案
ParseExact会告知其预期解析日期的格式,
ParseExact is told the format of the date it is expected to parse, not the format you wish to get out.
$invoice = '01-Jul-16'
[datetime]::parseexact($invoice, 'dd-MMM-yy', $null)
如果希望输出日期字符串:
If you then wish to output a date string:
[datetime]::parseexact($invoice, 'dd-MMM-yy', $null).ToString('yyyy-MM-dd')
Chris
这篇关于在PowerShell中将字符串转换为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文