Powershell's Get-date: How to get Yesterday at 22:00 in a variable?

前端 未结 7 1802
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 19:18

For a check i need yesterday\'s date at 10:00pm in a variable.

I get yesterdays Date and current Time with

$a = (get-date).AddDays(-1)
相关标签:
7条回答
  • 2020-12-30 19:59

    Format in other syntax is possible in this way

    [DateTime]::Today.AddDays(-1).ToString("yyyyMMdd")

    0 讨论(0)
  • 2020-12-30 20:01
    (Get-Date (Get-Date -Format d)).AddHours(-2)
    
    0 讨论(0)
  • 2020-12-30 20:01

    When I was to get yesterday with just the date in the format Year/Month/Day I use:

    $Variable = Get-Date((get-date ).AddDays(-1))  -Format "yyyy-MM-dd"
    
    0 讨论(0)
  • 2020-12-30 20:03

    Yet another way to do this:

    (Get-Date).AddDays(-1).Date.AddHours(22)
    
    0 讨论(0)
  • 2020-12-30 20:05

    I see this topic, but in my case I was looking for a way to improve the format. Using UFormat and adding -1 day

    (get-date (get-date).addDays(-1) -UFormat "%Y%m%d-%H%M")
    
    0 讨论(0)
  • 2020-12-30 20:06

    Use DateTime.Today as opposed to DateTime.Now (which is what Get-Date returns) because Today is just the date with 00:00 as the time, and now is the moment in time down to the millisecond. (from masenkablast)

    > [DateTime]::Today.AddDays(-1).AddHours(22)
    Thursday, March 11, 2010 10:00:00 PM
    
    0 讨论(0)
提交回复
热议问题