8

I'm not using yaml, because my company uses TFVC, so I need the classic way.

With $[pipeline.startTime] I get the starttime, but now I need it formated in this way: dd.MM.yyyy

A powershellscript like in VSO(TFS) - get current date time as variable helped me, but set the day directly in the variables would be a cleaner way

K.J.M.O.
  • 145
  • 2
  • 14
Steini
  • 405
  • 2
  • 4
  • 10
  • Are you trying to set the date on a pipeline variable and you are saying what you are setting is in the format you provided? Have you looked at this already? Using ParseExact. https://stackoverflow.com/questions/27741810/string-to-datetime-conversion-in-powershell – Matt Jul 09 '20 at 16:22
  • How about the issue? Does the answer below resolved your question, If not, would you please let me know the latest information about this issue? – Leo Liu Jul 10 '20 at 09:50
  • Does this answer your question? [Custom TFS Enviroment Variable doesn't read $(Date)](https://stackoverflow.com/questions/48094110/custom-tfs-enviroment-variable-doesnt-read-date) – Jim G. Oct 20 '22 at 14:33

3 Answers3

10

How can i set a Azure DevOps Pipeline Varible which contains the date in this Format: 25.07.2020

Since you are using the classic way, nested variables are not supported in the build pipeline. So, we could not use the variables like $(Get-Date -Format Date:MMddyy) to set the date time.

We could only set the variable like:

$[format('{0:ddMMyyyy}', pipeline.startTime)]

In this way, we could get the value 10072020, not the 10.07.2020 without .. And I could not add any interval between ddMMyyyy, it does not supported by Azure pipeline.

Besides, as workaround, we could defined the Build number format in the Options tab with value $(DayOfMonth).$(Month).$(Year:yyyy):

enter image description here

Then we could use variable $(Build.BuildNumber) directly to get the date time:

enter image description here

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • $currentDataTime = $(Get-Date -Format yyyy-MM-dd'T'HH:mm:ss) how to convert this format of date in Azure Pipeline from UTC to add 10 hrs? – Maddy Dec 07 '22 at 15:46
4

You can define a Azure DevOps Pipeline Variable by using expressions: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops It supports .NET custom date and time format specifiers:

$[format('{0:dd}.{0:MM}.{0:yyyy}', pipeline.startTime)]
korvalds
  • 41
  • 1
0

@ChamindaC's answer inspired my solution:

Write-Host "Setting up the date time for build variable"
$myDate=$(Get-Date -format yyyyMMdd-Hmmss)
Write-Host "##vso[task.setvariable variable=MyDate]$myDate"

And then later in my pipeline I can refer to $(MyDate).

enter image description here


For your specific date format, you could do this:

Write-Host "Setting up the date time for build variable"
$myDate=$(Get-Date -format dd.MM.yyyy)
Write-Host "##vso[task.setvariable variable=MyDate]$myDate"
Jim G.
  • 15,141
  • 22
  • 103
  • 166