茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1670906 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00206.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

微軟帝國 : [分享]如何用 Powershell 比對時間差

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[分享]如何用 Powershell 比對時間差
冷日為了因些因素,需要知道時間差,所以找了一下網路上的範例,這裡整理一下!

找到的第一篇是這個: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
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|