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

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

  • Comments 7
  • 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...
    (Date).AddDays(1).AddHours(2).AddMinutes(30)

  • @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