Get current Timestamp in CET format and concatenate with string in yml file

34 views Asked by At

I was able to get the current time but it fetches the time in another timezone(GMT) and I want to display the timezone in CET. What should I change in my script?

pwsh: |
  $date=$(Get-Date -format yyyyMMdd-Hmm)
  Write-Host "##vso[task.setvariable variable=currentTimeStamp]$date"
displayName: 'Getting timestamp'
1

There are 1 answers

1
Abdullah Khawer On BEST ANSWER

For CEST, change your code as follows:

pwsh: |
  $utcTimestamp = Get-Date -Format "yyyyMMdd-HHmm"
  $utcDateTimeObj = [DateTime]::ParseExact($utcTimestamp, "yyyyMMdd-HHmm", $null)
  $cetTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central European Standard Time")
  $cetDateTimeObj = [System.TimeZoneInfo]::ConvertTimeFromUtc($utcDateTimeObj, $cetTimeZone)
  $formattedCETTimestamp = $cetDateTimeObj.ToString("yyyyMMdd-HHmm")
  Write-Host "##vso[task.setvariable variable=currentCETTimestamp]$formattedCETTimestamp"
displayName: 'Getting timestamp'

For CET, change your code as follows:

pwsh: |
  $utcTimestamp = Get-Date -Format "yyyyMMdd-HHmm"
  $utcDateTimeObj = [DateTime]::ParseExact($utcTimestamp, "yyyyMMdd-HHmm", $null)
  $cetTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central European Standard Time")
  $cetDateTimeObj = [System.TimeZoneInfo]::ConvertTimeFromUtc($utcDateTimeObj, $cetTimeZone)
  $cetDateTimeObj = $cetDateTimeObj.AddHours(-1)
  $formattedCETTimestamp = $cetDateTimeObj.ToString("yyyyMMdd-HHmm")
  Write-Host "##vso[task.setvariable variable=currentCETTimestamp]$formattedCETTimestamp"
displayName: 'Getting timestamp'