简体   繁体   English

如何在控制 Powershell 中的格式的同时使用 get-date 更改月份

[英]How can I use get-date to change the month while controlling the format in Powershell

I just started working with Powershell and I am trying to manipulate a date such that I add two months to the date and set it as a var.我刚开始使用 Powershell 并且我正在尝试操作一个日期,以便在日期上添加两个月并将其设置为 var。 Powershell handles this nicely and even handles year rollover. Powershell 可以很好地处理这个问题,甚至可以处理年份翻转。 However I am stuck trying to figure out how to also control the format of the date before, during or after adding two months to the date.但是,我一直在试图弄清楚如何在将日期添加两个月之前、期间或之后控制日期的格式。 Both of the statements below give me what I want but i have not been able to figure out how to combine them.下面的两个陈述都给了我想要的东西,但我无法弄清楚如何将它们结合起来。

$ndate = (Get-Date).AddMonths(2) $ndate = (Get-Date).AddMonths(2)

$exp = date -format MM/dd/yyyy $exp = 日期格式 MM/dd/yyyy

Thank you,谢谢,

Get-Date (Get-Date).AddMonths(2) -f MM/dd/yyyy

When the -Format operator is not avaiable you can use the ToString method:当 -Format 运算符不可用时,您可以使用 ToString 方法:

(Get-Date).AddMonths(2).ToString('MM/dd/yyyy')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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