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:MMM}.{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)"
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:
trigger:
- '*'
pool: linux
variables:
time: $[ format('{0:yyyy}-{0:MM}-{0:dd}', pipeline.startTime) ]
pwsh_time: $(Get-Date -Date $(time) -Format yyyy.MMM.dd)
steps:
- pwsh: echo $(var1)
- bash: echo $(var1) //error: line 1: Get-Date: command not found
This is modified version with output variables that solves above problem:
trigger:
- '*'
pool: linux
variables:
time: $[ format('{0:yyyy}-{0:MM}-{0:dd}', pipeline.startTime) ]
pwsh_time: $(Get-Date -Date $(time) -Format yyyy.MMM.dd)
jobs:
- job: Prep
steps:
- pwsh: |
echo "##vso[task.setvariable variable=out;isoutput=true]$(pwsh_time)"
name: format_time
- job: B
dependsOn: Prep
variables:
formated_time: $[ dependencies.Prep.outputs['format_time.out'] ]
steps:
- bash: |
echo $(formated_time)