简体   繁体   English

日期/时间在Powershell中无法正确格式化

[英]Date / Time won't format correctly in Powershell

I'm having issues converting the date to just 02/04/2016 in my power shell script, it is finding when the DAT file for McAfee is created. 我在Power Shell脚本中将日期转换为02/04/2016时遇到问题,它发现为McAfee创建DAT文件的时间。 For some reason it is outputting 02/03/2016 00:00:00, can anyone tell me why it is adding the 00:00:00? 由于某种原因,它正在输出02/03/2016 00:00:00,有人可以告诉我为什么要添加00:00:00吗? Here's what i have so far: 这是我到目前为止的内容:

$mcafee = Get-ItemProperty HKLM:\SOFTWARE\Wow6432Node\McAfee\AVEngine
$mcafee.AvDatDate
$datfile=[datetime]$mcafee.AvDatDate

$datfile.adddays(-3)
$threedaysbefore = $datfile.adddays(-3)

$WPFMcAfeeField.Text = $datfile

$datfile.adddays(-3)
$threedaysbefore = $datfile.adddays(-3)


$WPFMcAfeeField.Text = $datfile

So my real question is, how can i edit this statement as a whole to where it just outputs in the field: 02/03/2016? 所以我真正的问题是,如何将该语句整体编辑为字段在以下位置的输出:02/03/2016? I've tried everything i can think of. 我已经尝试了所有我能想到的。 Do i need to reformat it? 我需要重新格式化吗?

With [datetime]$mcafee.AvDatDate you are casting to a [datetime] object. 使用[datetime]$mcafee.AvDatDate可以强制转换为[datetime]对象。 What you see is to be expected for the value $datfile . 您所看到的应该是$datfile值。 It will contain a date and time . 它将包含一个日期和时间 Since you have not provided values it defaults to midnight. 由于您未提供值,因此默认为午夜。

If you only want to use the date then you can use various methods to just return that data. 如果只想使用日期,则可以使用各种方法来返回该数据。 A simple case is the ToString() method. 一个简单的例子是ToString()方法。 Provide it a date time format and it will return the matching string. 提供一个日期时间格式 ,它将返回匹配的字符串。

PS C:\temp> $datfile.ToString("MM/dd/yyyy",[cultureinfo]::InvariantCulture)
02/04/2016

Setting the culture setting is important to ensure you get the expected result from the string. 设置区域性设置对于确保您从字符串中获得预期的结果很重要。 In the case above it is to be sure that your regional settings do interfere with the output or what it calls culture insensitive. 在上述情况下,请确保您的区域设置确实干扰了输出或所谓的对文化不敏感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM