PowerTip: Add Days, Hours, and Minutes to Current Time

PowerTip: Add Days, Hours, and Minutes to Current Time

  • Comments 9
  • Likes

Summary: Use Windows PowerShell to easily add days, hours, and minutes to the current time.

Hey, Scripting Guy! Question How can I use Windows PowerShell to add one day, two hours, and thirty minutes to the current date and time?

Hey, Scripting Guy! Answer Create a TimeSpan object that represents one day, two hours, and thirty minutes,
          then add it to the current date and time that Get-Date retrieves:

$ts = New-TimeSpan -Days 1 -Hours 2 -Minutes 30

(get-date) + $ts

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • OR...

  • @Pitor now that is cool. I did not know that I could do that. Thank you for sharing.

  • Fastest methos [DateTime]::Now.Add(1).AddHours(2).AddMinutes(30) - 40 Milliseconds of execution time

  • Thank you all including Chen and Piotr as well!

  • thank you

  • When I try the Set_date command, I always get this error:
    Set-Date : A required privilege is not held by the client
    At line:1 char:9
    + set-date <<<<
    + CategoryInfo : NotSpecified: (:) [Set-Date], Win32Exception
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.SetDateCommand

    Can't find any documentation on how to modify PS privileges.

  • Really good! Did not know about New-TimeSpan; I used Get-Date

  • Is there a way to do something similar to : $dayofweek = (get-date).AddDays(+7) | Select DayOfWeek; $short = (Get-Date).AddDays(+7).ToString('yyyy-mm-dd HH:mm AM/PM') because when I try I get A1/P1. Specifically, I want to add 7 days from Monday, then Tue-Sun add 1,2,3,4,5,6 days. But keep the Hour set at 1:00 AM. Any ideas?

  • $d=Get-Date

    works as well!