我想在 Azure DevOps yaml 中使用日期变量/参数作为管道,但我很难找到一种方法来获取月份短名称而不需要重新发明轮子。
我在这里发现了这个问题:如何获取 Azure Pipeline YAML 文件中的当前日期以使用变量?
所以我得到了格式并将其更改为使用“MMM”而不是像这样的“MM”
variables:
currentDate: $[ format('{0:yyyy}.{0:MM}.{0:dd}', pipeline.startTime) ]
但是当我执行它时会抛出错误:
格式说明符“MMM”对于“DateTime”类型的对象无效
根据 表达式 - Azure Pipelines |微软学习,
使用 .NET 自定义日期和时间格式说明符进行日期格式设置 (
、yyyy
、yy
、MM
、M
、dd
、d
、HH
、H
、m
、mm
、ss
、s
、f
、ff
、ffff
)K
因此,格式说明符“MMM”对于“DateTime”类型的对象无效
不确定为什么要使用
MMM
,但这里有一个解决方法供您参考,因为 MMM
输出始终以 0
开头。
variables:
currentDate: $[ format('{0:yyyy}.{0:MM}.{0:dd}', pipeline.startTime) ]
MMMDate: $[ format('{0}.0{1}.{2}', format('{0:yyyy}', pipeline.startTime), format('{0:MM}', pipeline.startTime), format('{0:dd}', pipeline.startTime) ) ]
steps:
- script:
echo $(MMMDate)