string PowerShell 中的字符串到日期时间转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27741810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String to DateTime conversion in PowerShell
提问by Renji
I am trying to convert the time stamp value of a file to date time for comparing two date time differences.
我正在尝试将文件的时间戳值转换为日期时间以比较两个日期时间差。
The data would be coming in format 24122014_022257. I need to convert this into date time so that I can compare the values:
数据将采用24122014_022257格式。我需要将其转换为日期时间,以便我可以比较这些值:
$usdate="24122014_022257"
$dateParts = $usdate -split "_"
$final = $dateparts[0] + $dateParts[1]
How can I do it?
我该怎么做?
回答by mjolinor
You can use the ParseExact method:
您可以使用 ParseExact 方法:
[datetime]::ParseExact('24122014_022257','ddMMyyyy_HHmmss',$null)
Wednesday, December 24, 2014 2:22:57 AM
回答by Adam Luniewski
ParseExact will do just that. You can specify custom datetime format in second parameter. You can leave third parameter as null, or specify a culture to use (it may matter if you're importing files that were generated from system with different time zone).
ParseExact 将做到这一点。您可以在第二个参数中指定自定义日期时间格式。您可以将第三个参数保留为 null,或指定要使用的区域性(如果您要导入从具有不同时区的系统生成的文件,则可能很重要)。
$usdate="24122014_022257"
[datetime]::ParseExact($usdate,"ddMMyyyy_HHmmss", [System.Globalization.CultureInfo]::CurrentCulture)
回答by Abhishek Singha
get-date 24122014022257
获取日期 24122014022257
Wednesday, December 24, 2014 2:22:57 AM
2014 年 12 月 24 日,星期三 2:22:57