0

I need to get the current date on a windows workflow into an env variable.

Obviously, this is not working :

- name: Set current date as env variable
  run: echo "NOW=Get-Date -format "yyyy-MM-dd"" >> $GITHUB_ENV

All example I found are running on linux :(
How to to this on a windows runner ?

Julien
  • 1,810
  • 1
  • 16
  • 34
  • Does this answer your question? [Github actions: set environment variable for Windows build with PowerShell](https://stackoverflow.com/questions/66733076/github-actions-set-environment-variable-for-windows-build-with-powershell) – Azeem Jul 02 '23 at 06:42
  • `$NOW=& Get-Date -format yyyy-mm-dd` and then `echo "NOW=$NOW" >> $env:GITHUB_ENV` should work for your specific use case. – Azeem Jul 02 '23 at 07:00
  • @Azeem https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#example-of-writing-an-environment-variable-to-github_env I tried your version and also lightly modified version based on the link `$NOW=& Get-Date -format yyyy-mm-dd` and then echo `"NOW=$NOW" >> "$GITHUB_ENV"` but `echo "${{env.NOW}}"` is still empty... – Julien Jul 02 '23 at 08:31
  • Please edit your question and update it with the version that you tried. In your above comment, you're using `$GITHUB_ENV` which works in Bash/shell, not in Powershell. In Powershell, use `$env:GITHUB_ENV`. – Azeem Jul 02 '23 at 08:34
  • 1
    I finally made it work. I had several stacked problems. 1 missing skill in powershell , 2 use `$env:GITHUB_ENV`, 3 YOU NEED TO SET AND USE IN DIFERENT `-run` Thank you for your help – Julien Jul 02 '23 at 08:37
  • Glad to hear that. Yes, an env var set in one step is available in subsequent steps under the same job. You're welcome! – Azeem Jul 02 '23 at 08:42

1 Answers1

0

There is a simple GitHub Action that can return the current date and time in the specified format - Current date time.

For example:

- name: Test1
  id: t1
  uses: Kaven-Universe/github-action-current-date-time@v1
  with:
    format: "yyyy-MM-dd"

# Use the output from the `t1` step
- name: Output1
  run: |
    echo "The time was ${{ steps.t1.outputs.time }}"
    echo "The year was ${{ steps.t1.outputs.year }}"
    echo "The month was ${{ steps.t1.outputs.month }}"
    echo "The day was ${{ steps.t1.outputs.day }}"
    echo "The hours was ${{ steps.t1.outputs.hours }}"
    echo "The minutes was ${{ steps.t1.outputs.minutes }}"
    echo "The seconds was ${{ steps.t1.outputs.seconds }}"
    echo "The milliseconds was ${{ steps.t1.outputs.milliseconds }}"
    echo "The day_of_week was ${{ steps.t1.outputs.day_of_week }}"
    echo "The week_of_year was ${{ steps.t1.outputs.week_of_year }}"
    echo "The milliseconds_since_epoch was ${{ steps.t1.outputs.milliseconds_since_epoch }}"

The example above demonstrates also the action outputs available. The values of these outputs can be accessed in other steps within the current job as well as from another job. See the Defining outputs for jobs for more details.

Andrii Bodnar
  • 1,672
  • 2
  • 17
  • 24