如何在 PowerShell 中格式化日期时间 [英] How to format a DateTime in PowerShell

查看:103
本文介绍了如何在 PowerShell 中格式化日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以格式化 Get-Date cmdlet 没有这样的问题:

I can format the Get-Date cmdlet no problem like this:

$date = Get-Date -format "yyyyMMdd"

但是一旦我有了 变量中的日期,如何格式化?声明如下

But once I've got a date in a variable, how do I format it? The statement below

$dateStr = $date -format "yyyMMdd"

返回此错误:

"您必须提供一个值表达式在-f"的右侧运算符"

"You must provide a value expression on the right-hand side of the '-f' operator"

推荐答案

与您在 .NET:

$DateStr = $Date.ToString("yyyyMMdd")

或者:

$DateStr = '{0:yyyyMMdd}' -f $Date

这篇关于如何在 PowerShell 中格式化日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆