简体   繁体   中英

Powershell Get-Date -Day (New Year)

Powershell Get-Date Explanation:

-Day

Specifies the day of the month that is displayed. Enter a value from 1 to 31. >The default is the current day.

If you specify a value that is greater than the number of days in the month, >PowerShell adds the number of days to the month and displays the result. For >example, "Get-Date -Month 2 -Day 31" displays "March 3", not "February 31".

Will this roll over into new year as well, or do I have to adjust code to detect new year if the date ends up transforming into January 1 ?

No, it will not roll over, because December has 31 days and anything higher than 31 is an exception.

Get-Date -Month 2 -Day 32

Result:

Get-Date : Cannot validate argument on parameter 'Day'. The 32 argument is greater than the maximum allowed range of 31. Supply an argument that is less than or equal to 31 and then try the command again.
At line:1 char:24
+ get-date -month 2 -day 32
+
+ CategoryInfo : InvalidData: (:) [Get-Date], ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetDateCommand

Same for month 12:

Get-Date -Month 12 -Day 32

Result

Get-Date : Cannot validate argument on parameter 'Day'. The 32 argument is greater than the maximum allowed range of 31. Supply an argument that is less than or equal to 31 and then try the command again.
At line:1 char:24
+ get-date -month 2 -day 32
+
+ CategoryInfo : InvalidData: (:) [Get-Date], ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetDateCommand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM