从字符串到日期PowerShell

哈德里安·博吉恩

我在编码方面还很陌生。这就是我在PowerShell中遇到问题的要点。

我从csv中获取了一个变量,该变量称为 $datefinder = (Get-Date -Format "yyyy/MM/dd HH:mm")

我在循环中得到了该变量的正确答案,但是在同一循环中,我还需要30天的变量$ datefinder。

我试图创建一个新的变量,如:

$2ndeDate = $datefinder.adddays(+30)

我的变量datefinder是一个字符串,因此我需要将其传递给date。我已经尝试了很多东西。

这是csv文件的第一行

UCB63_DATENUM;U6618_FILENAME;UF6E8_CANAL;U65B8_IDRP
7/8/19 22:27;457E6659_ZN_LIQRLVPR_A_V_ML.pdf;ML;1367091
根据

将日期变量用作真实的DateTime对象始终是一个好主意仅当出于某种原因需要格式化它们时,才将它们转换为某种格式的字符串。

幸运的是,相反的情况也是可能的。
根据您刚才给出的示例,您需要像这样从CSV解析日期:

$datefinder = [datetime]::ParseExact('7/8/19 19:18', 'M/d/yy HH:mm', $null)
# gets you the date Monday, July 8 2019 19:18:00

或像这样:

$datefinder = [datetime]::ParseExact('7/8/19 19:18', 'd/M/yy HH:mm', $null)
# gets you the date Wednesday, August 7 2019 19:18:00

将此日期作为DateTime对象,可以使用其方法AddDays()添加天数:

$2ndDate = $datefinder.AddDays(30)  # 30 days in the future as of the date in $datefinder

要么

$2ndDate = (Get-Date).AddDays(-30)  # 30 days in the past

希望能有所帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章