使用 PowerShell 的日期差异 [英] Difference between dates using PowerShell
本文介绍了使用 PowerShell 的日期差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习 Windows PowerShell,并且我正在尝试获取给定两个日期的月差:
I am learning Windows PowerShell, and I am trying to get the months difference given two dates:
For example: end: 2014-06-01
now: 2014-01-10
answer: 6 months
$now = get-date
$date = $end -as [DateTime];
if (!$date)
{
'You entered an invalid date'
}
else
{
$nts = ($now - $end).Months
write-output $nts
}
我已经尝试了所有方法,但无法在 PowerShell 中获得正确的月份.我该如何解决?
I have tried everything, and I can't get the correct months in PowerShell. How can I fix it?
推荐答案
类似的事情?
$begin = [datetime]'02/10/2011'
$end = [datetime]'01/10/2013'
$monthdiff = $end.month - $begin.month + (($end.Year - $begin.year) * 12)
$monthdiff
23
这篇关于使用 PowerShell 的日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文