Forum Discussion
tehatchKFI
Sep 28, 2023Copper Contributor
Get-date AddDays Method Not Returning a Value
I have an unusual one. The AddDays method of Get-Date isn't returning a value on my computer. It's not working in PowerShell 5 or 7. Get-date by itself works find and returns today's date. PS...
- Sep 28, 2023
In line six of your original post, you have made a small syntax mistake, which is why it returns nothing.
From line 6, this:
(get-date).AddDays.(-30)
Should have been this (note the removal of the period prior to the "(-30)"):
(get-date).AddDays(-30)
Cheers,
Lain
Sep 28, 2023
No, but does (Get-date).AddDays(-30).ToString('dd-MM-yyyy') work?
tehatchKFI
Sep 28, 2023Copper Contributor
Ok this is wild. It wasn't returning a value. I tried your suggestion, and now it seems to be working fine?
PowerShell 7.3.7
PS C:\Users\me> (get-date).AddDays.(-30)
PS C:\Users\me> (get-date)
Thursday, September 28, 2023 4:05:39 PM
PS C:\Users\me> (Get-date).AddDays(-30).ToString('dd-MM-yyyy')
29-08-2023
PS C:\Users\me> (get-date).AddDays(10)
Sunday, October 8, 2023 4:18:14 PM
PS C:\Users\me> (get-date).AddDays(-10)
Monday, September 18, 2023 4:18:19 PM
PS C:\Users\me> (get-date).AddDays(-30)
Tuesday, August 29, 2023 4:18:23 PM
PS C:\Users\me>
PowerShell 7.3.7
PS C:\Users\me> (get-date).AddDays.(-30)
PS C:\Users\me> (get-date)
Thursday, September 28, 2023 4:05:39 PM
PS C:\Users\me> (Get-date).AddDays(-30).ToString('dd-MM-yyyy')
29-08-2023
PS C:\Users\me> (get-date).AddDays(10)
Sunday, October 8, 2023 4:18:14 PM
PS C:\Users\me> (get-date).AddDays(-10)
Monday, September 18, 2023 4:18:19 PM
PS C:\Users\me> (get-date).AddDays(-30)
Tuesday, August 29, 2023 4:18:23 PM
PS C:\Users\me>
- Sep 28, 2023That is odd... Language/Culture setting, not sure why it's working now.. Even after opening another session?
- tehatchKFISep 28, 2023Copper ContributorYep, even rebooted, it's working fine now.
- Sep 28, 2023Good to hear, please mark my answer as solution to mark it as solved