powershell - 日期时间比较不匹配

标签 powershell datetime

我想将从xml文件获得的日期与文件的“lastwritetime”进行比较。 Powershell ISE对两者都显示相同的内容,但是我的if不返回true。我尝试了一些日期时间转换,但这也无济于事。

我的XML:

<meta>
    <log path="D:\tomcat.log">
        <lastwrite>08/03/2015 13:44:09</lastwrite>
    </log>
</meta>

我的powershell脚本:
[xml]$log_meta = Get-Content "D:\log_meta.xml"
$node = $log_meta.meta.log | where {$_.path -eq "D:\tomcat.log"}

(ls "D:\tomcat.log").LastWriteTime
$node.lastwrite

if((ls "D:\tomcat.log").LastWriteTime -eq $node.lastwrite){
    "Date and time is the same"
}

第三行显示:(德语日期格式)
Montag, 3. August 2015 13:44:09

第四行显示:
08/03/2015 13:44:09

但是我的if不返回true。

最佳答案

$ node.lastwrite是一个字符串(您从文件中读取),而LastWriteTime是DateTime。您需要将它们都转换为日期,或者都转换为DateTime。

如果将两者都转换为DateTime,则需要将文件时间四舍五入到最接近的秒。在NTFS上,文件时间的精度为100纳秒。

最好将文件时间转换为相同格式的字符串。

$writeTimeString = (ls "D:\tomcat.log").LastWriteTime.ToString("MM/dd/yy HH:mm:ss")

关于powershell - 日期时间比较不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31802277/

相关文章:

powershell - 如何从 Azure Powershell 重新启动 Azure 应用服务

asp.net-mvc - 由 TeamCity 触发的构建步骤始终会构建 - 即使没有任何更改

ruby - ruby 中的时间格式化字符串

datetime - solr搜索时间范围内的时间

php - 如何在php中分隔日期和时间?

powershell - 在powershell中将参数传递给Set-Alias的正确方法是什么?

windows - 从bitvise SSH服务器控制面板导出Windows帐户列表

Powershell - 使用 Export-csv 为多个数组对象创建新行

c# - 从 DateTime 中删除时间

Python/ Pandas 类型错误 : 'list' object is not callable