對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2016/1/23 9:07:57
冷日為了因些因素,需要知道時間差,所以找了一下網路上的範例,這裡整理一下!
找到的第一篇是這個:PowerTip: Get Time Difference between Dates with PowerShell - Hey, Scripting Guy! Blog - Site Home - TechNet Blogs
Get Time Difference between Dates with PowerShell
Summary: Use New-TimeSpan to produce the time difference between two dates.
Hey, Scripting Guy! Question I know I can subtract dates, but is there a Windows PoweShell cmdlet to show me the time difference between dates?
Hey, Scripting Guy! Answer Use New-TimeSpan and supply the information!
For example, to show the difference between today and the upcoming New Year:
冷日:
看起來就是利用『GET-DATE』,然後可以直接減?
恩,應該不難!
然後是這篇:Difference between dates using powershell - Stack Overflow
Difference between dates using powershell
I am learning windows powershell and I am trying to get the months difference given two dates:
I have tried everything, and I can't get the correct months in powershell. any ideas will be much appreciated.
Something like this?
Amazing!! thanks this worked like a charm!! – Menew Jan 10 '14 at 19:44
Where is the $endFromDatabase variable coming from? That part isn't clear in your post.
To demonstrate subtracting DateTime objects, see the example below.
You will receive a System.TimeSpan as a result from the operation, so you can explore members such as:
My guess is that you just need to fix the value in the $endFromDatabase variable, so that it can be cast into a System.DateTime object.
冷日:
這裡冷日看到的關鍵應該是
也就是說,Get-Date 以後,可以直接取用他的一些常數,我們就可以直接取秒、分、時、日、月、年的差額!
恩,看你需要的是啥囉!
而後看到的是這篇:Time Difference in Powershell - Spiceworks
Time Difference in Powershell
I wanted to post this solution to a Powershell problem I had because I couldn't find any examples on the internet.
I needed to be able to calculate the difference between a timestamp from the last 24 hours, and the current time.
The input and output formats needed to be hh:mm:ss
Here's how I did it:
I hope this helps someone else.
冷日:
基本上他把冷日上一篇所說的的時(Hours)分(Minutes)秒(Seconds)都說得很清楚了,看來真的不難!
最後就補上官方的說明文件:https://technet.microsoft.com/zh-tw/library/hh849887.aspx
找到的第一篇是這個:PowerTip: Get Time Difference between Dates with PowerShell - Hey, Scripting Guy! Blog - Site Home - TechNet Blogs
Get Time Difference between Dates with PowerShell
Summary: Use New-TimeSpan to produce the time difference between two dates.
Hey, Scripting Guy! Question I know I can subtract dates, but is there a Windows PoweShell cmdlet to show me the time difference between dates?
Hey, Scripting Guy! Answer Use New-TimeSpan and supply the information!
For example, to show the difference between today and the upcoming New Year:
$StartDate=(GET-DATE)
$EndDate=[datetime]”01/01/2014 00:00”
NEW-TIMESPAN –Start $StartDate –End $EndDate
冷日:
看起來就是利用『GET-DATE』,然後可以直接減?
恩,應該不難!
然後是這篇:Difference between dates using powershell - Stack Overflow
Difference between dates using powershell
I am learning windows powershell and I am trying to get the months difference given two dates:
$now = get-date
$date = $end -as [DateTime];
if (!$date)
{
'You entered an invalid date'
}
else
{
$nts = ($now - $end ).Months
write-output $nts
}
I have tried everything, and I can't get the correct months in powershell. any ideas will be much appreciated.
Something like this?
$begin = [datetime]'02/10/2011'
$end = [datetime]'01/10/2013'
$monthdiff = $end.month - $begin.month + (($end.Year - $begin.year) * 12)
$monthdiff
Amazing!! thanks this worked like a charm!! – Menew Jan 10 '14 at 19:44
Where is the $endFromDatabase variable coming from? That part isn't clear in your post.
To demonstrate subtracting DateTime objects, see the example below.
$TimeSpan = [DateTime]'2014-01-10' - [DateTime]'2014-01-06';
You will receive a System.TimeSpan as a result from the operation, so you can explore members such as:
$TimeSpan.TotalDays;
My guess is that you just need to fix the value in the $endFromDatabase variable, so that it can be cast into a System.DateTime object.
冷日:
這裡冷日看到的關鍵應該是
$end.month - $begin.month
也就是說,Get-Date 以後,可以直接取用他的一些常數,我們就可以直接取秒、分、時、日、月、年的差額!
恩,看你需要的是啥囉!
而後看到的是這篇:Time Difference in Powershell - Spiceworks
Time Difference in Powershell
I wanted to post this solution to a Powershell problem I had because I couldn't find any examples on the internet.
I needed to be able to calculate the difference between a timestamp from the last 24 hours, and the current time.
The input and output formats needed to be hh:mm:ss
Here's how I did it:
$Time1 = "15:30:00"
$Time2 = Get-Date -format HH:mm:ss
$TimeDiff = New-TimeSpan $Time1 $Time2
if ($TimeDiff.Seconds -lt 0) {
$Hrs = ($TimeDiff.Hours) + 23
$Mins = ($TimeDiff.Minutes) + 59
$Secs = ($TimeDiff.Seconds) + 59 }
else {
$Hrs = $TimeDiff.Hours
$Mins = $TimeDiff.Minutes
$Secs = $TimeDiff.Seconds }
$Difference = '{0:00}:{1:00}:{2:00}' -f $Hrs,$Mins,$Secs
$Difference
I hope this helps someone else.
冷日:
基本上他把冷日上一篇所說的的時(Hours)分(Minutes)秒(Seconds)都說得很清楚了,看來真的不難!
最後就補上官方的說明文件:https://technet.microsoft.com/zh-tw/library/hh849887.aspx