Powershell Get-Date说明:
-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".
如果日期最终转换为
January 1
,这是否还会延续到新年,还是我必须调整代码以检测新年?
最佳答案
不,它不会过期,因为12月有31天,任何高于31天的情况都是异常(exception)。
Get-Date -Month 2 -Day 32
结果:
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
第12个月相同:
Get-Date -Month 12 -Day 32
结果
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
关于.net - Powershell Get-Date -Day(新年),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53806430/