如何在PowerShell中格式化DateTime? [英] How to format a DateTime in PowerShell?

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

问题描述

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

I can format the Get-Date commandlet 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'
操作符右侧的b
$ b

"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中格式化DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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