skip to Main Content

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


  1. According to Expressions – Azure Pipelines | Microsoft Learn,

    Uses .NET custom date and time format specifiers for date formatting (yyyy, yy, MM, M, dd, d, HH, H, m, mm, ss, s, f, ff, ffff, K)

    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, given MMM output will always begin with 0.

    variables:
      currentDate: $[ format('{0:yyyy}.0{0:MM}.{0:dd}', pipeline.startTime) ]
    
    steps:
    - script: 
        echo $(currentDate)
    

    enter image description here

    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search