.net - Powershell Get-Date -Day(新年)

标签 .net powershell

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/

相关文章:

C# - List<T>.Remove() 总是删除列表中的第一个对象

c# - 使用 Visual Basic.NET (VB.NET) 开发的 ASP.NET Core 应用程序可能吗? VS中没有模板

.net - 由于 2 秒超时,并非所有 native 全局变量都在混合模式 .Net 应用程序中被破坏

powershell - 从没有临时文件的 URL 运行 Powershell 脚本

powershell - 如果 Get-ChildItem 失败,如何在 powershell 中检查?

将电影标题排序为 CSV

c# - "is"关键字和 Equals 方法的覆盖

c# - 检查 Windows 8 应用程序是否在分配的访问模式下运行

powershell - 循环到脚本

powershell - 在 NuGet 包安装/初始化期间运行的 Powershell 脚本有哪些安全限制?