2

I wish to set the System.DateTime object below with the time from $time:

$CurrentTime = (date)
$Time = "9:00"

How do I get the date from $CurrentTime, but with hours, minutes, seconds from $time?

4 Answers 4

8

Try this:

[datetime]::ParseExact("09:00","hh:mm",$null)
Sign up to request clarification or add additional context in comments.

1 Comment

I like the one liner. Thanks
1
Get-Date -Year $CurrentTime.Year `
         -Month $CurrentTime.Month `
         -Day $CurrentTime.Day `
         -Hour $time.split(':')[0] `
         -Minute $time.split(':')[-1]
         -Second $time.split(':')[-1]

1 Comment

You can introduce them as well via the -Seconds parameter, I will update my answer.
1

This should work:

$CurrentTime = [System.DateTime]::Parse((date).ToString("yyyy.MM.dd"))
$Time = [System.Timespan]::Parse("9:00")
$Result = $CurrentTime.Add($Time)

Comments

1
$CurrentTime = Get-Date -Hour "09" -Minute "00" -Second "00"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.