I want to use a date variable/param in an Azure DevOps yaml for a pipeline and i am having difficulty in finding a way to get the month short name without reinventing the wheel.
I have found this question here: How can I get the current date in an Azure Pipeline YAML file to use ase a variable?
so i got the format and changed it to use "MMM" instead of "MM" like so
variables:
currentDate: $[ format('{0:yyyy}.{0:MM}.{0:dd}', pipeline.startTime) ]
but this is throwing an error when i execute it:
The format specifiers ‘MMM’ are not valid for objects of type ‘DateTime’
My expectation would be to have the formatting be something like 25.Dec.2024 – just a display thing, i am using that date to pass on to an app execution as a string run argument, example:
variables:
currentDate: $[ format('{0:yyyy}.{0:MM}.{0:dd}', pipeline.startTime) ]
executionTitle: "My Test Execution $(currentDate)"
2
Answers
According to Expressions – Azure Pipelines | Microsoft Learn,
Therefore, the format specifiers ‘MMM’ are not valid for objects of type ‘DateTime’
Not sure why you would like to use
MMM
, but here is a workaround for your reference, givenMMM
output will always begin with0
.As you noticed, Azure DevOps does not allow you to provide such a format. It is possible to change the formatting by custom process, but this is a workaround. In the case of the given example, the pipeline becomes more complicated and also the formatting is platform-dependent (if you are not using cross-platform tools). Simpler version – works with Powershell tasks:
This is modified version with output variables that solves above problem: