DEV Community

Olga Braginskaya
Olga Braginskaya

Posted on • Originally published at datobra.com on

Handling Dates in Argo Workflows

Working with dates in Argo Workflows can range from simple to unexpectedly challenging depending on your needs. Whether you're scheduling tasks, extracting specific parts of a date, or looping through hours for complex workflows there’s plenty you can achieve—but figuring it out might take some trial and error. In this post I’ll share practical tips and examples to help you navigate date handling more easily.

If you’re just starting with Argo Workflows I’ve written a guide to setting up workflows locally. Feel free to check it out—it’s a great place to start before diving into date operations. (For reference, I’m using Argo Workflows v3.5.11 in this post.)

Using workflow.creationTimestamp

Argo provides a built-in variable {{workflow.creationTimestamp}} which gives you the exact time when the workflow was created. This variable is extremely useful for creating timestamps or formatting dates.

- name: current-date
  value: '{{workflow.creationTimestamp}}'

Enter fullscreen mode Exit fullscreen mode

Example: Breaking It Down

Want to extract just the parts of the date? No problem:

- name: year
  value: '{{workflow.creationTimestamp.Y}}'
- name: month
  value: '{{workflow.creationTimestamp.m}}'
- name: day
  value: '{{workflow.creationTimestamp.d}}'
- name: hour
  value: '{{workflow.creationTimestamp.H}}'
- name: minute
  value: '{{workflow.creationTimestamp.M}}'
- name: second
  value: '{{workflow.creationTimestamp.S}}'

Enter fullscreen mode Exit fullscreen mode

Adding Magic with the Sprig Library

If workflow.creationTimestamp helps you get started, the Sprig library helps you do more with dates like formatting them or calculating new ones. Let’s look at how you can format workflow.creationTimestamp using Sprig.

Example: Formatting the Creation Timestamp

Suppose you want to turn workflow.creationTimestamp into a simpler format like YYYY-MM-DD. The Sprig library makes this straightforward:

- name: creation-timestamp-formatted
  value: '{{=sprig.date("2006-01-02", workflow.creationTimestamp)}}'

Enter fullscreen mode Exit fullscreen mode

Note : The Sprig library uses Go’s date formatting style, where a specific reference date (Mon Jan 2 15:04:05 MST 2006) serves as the template. For example:

  • 2006 represents the year.
  • 01 represents the month.
  • 02 represents the day.
  • 15:04 represents hours and minutes in 24-hour time.

If you need additional formatting (e.g., including time), you can adjust the format string accordingly:

  • 2006-01-02 15:04:052025-01-02 14:35:00

Example: Getting Yesterday's Date

Need to backtrack a bit? Here’s how to get yesterday’s date:

- name: yesterday
  value: '{{=sprig.date("2006-01-02", sprig.dateModify("-24h", sprig.now()))}}'

Enter fullscreen mode Exit fullscreen mode

What’s happening here?

  • 2006-01-02 is Sprig’s standard date format. (It’s a bit quirky, but you’ll get used to it!)
  • -24h shifts the date back by 24 hours.

Example: Extracting Date Parts from Modified Dates

What if you want the year of yesterday’s date? Or the month? No problem:

- name: year
  value: '{{=sprig.date("2006", sprig.dateModify("-24h", sprig.now()))}}'
- name: month
  value: '{{=sprig.date("01", sprig.dateModify("-24h", sprig.now()))}}'
- name: day
  value: '{{=sprig.date("02", sprig.dateModify("-24h", sprig.now()))}}'

Enter fullscreen mode Exit fullscreen mode

Example: Converting Dates to Epoch Time

Need to work with epoch time? Sprig has you covered:

- name: parsedEpoch
  value: "{{=sprig.unixEpoch(sprig.toDate('2006-01-02T15:04:05 MST', inputs.parameters.inputDate))}}"
- name: nowEpoch
  value: "{{=sprig.unixEpoch(sprig.now())}}"

Enter fullscreen mode Exit fullscreen mode

A Sample Workflow for Date Handling

To bring it all together, here’s a sample workflow that demonstrates how to extract and manipulate various parts of a date.

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  name: date-example
spec:
  entrypoint: main
  arguments:
    parameters:
      - name: creation-timestamp
        value: '{{workflow.creationTimestamp}}'
      - name: today-year
        value: '{{workflow.creationTimestamp.Y}}'
      - name: today-month
        value: '{{workflow.creationTimestamp.m}}'
      - name: today-day
        value: '{{workflow.creationTimestamp.d}}'
      - name: creation-timestamp-formatted
        value: '{{=sprig.date("2006-01-02", workflow.creationTimestamp)}}'
      - name: yesterday
        value: '{{=sprig.date("2006-01-02", sprig.dateModify("-24h", sprig.now()))}}'
      - name: year
        value: '{{=sprig.date("2006", sprig.dateModify("-24h", sprig.now()))}}'
      - name: month
        value: '{{=sprig.date("01", sprig.dateModify("-24h", sprig.now()))}}'
      - name: day
        value: '{{=sprig.date("02", sprig.dateModify("-24h", sprig.now()))}}'
      - name: now-epoch
        value: "{{=sprig.unixEpoch(sprig.now())}}"

  templates:
    - name: main
      inputs:
        parameters:
          - name: creation-timestamp
          - name: today-year
          - name: today-month
          - name: today-day
          - name: creation-timestamp-formatted
          - name: yesterday
          - name: year
          - name: month
          - name: day
          - name: now-epoch
      container:
        image: busybox
        command: [sh, -c]
        args:
          - |
            echo "Today's date: {{inputs.parameters.creation-timestamp}}";
            echo "Today's year: {{inputs.parameters.today-year}}";
            echo "Today's month: {{inputs.parameters.today-month}}";
            echo "Today's day: {{inputs.parameters.today-day}}";
            echo "Today's date formatted: {{inputs.parameters.creation-timestamp-formatted}}";
            echo "Yesterday's date: {{inputs.parameters.yesterday}}";
            echo "Year: {{inputs.parameters.year}}";
            echo "Month: {{inputs.parameters.month}}";
            echo "Day: {{inputs.parameters.day}}";
            echo "Now-epoch: {{inputs.parameters.now-epoch}}";

Enter fullscreen mode Exit fullscreen mode

And what it prints:

Looping Through Hours (Because Why Not?)

Sometimes you need to iterate through all the hours of the day—whether it’s for hourly jobs or logging. Argo’s withSequence makes this painless:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: loop-hours-example-
spec:
  entrypoint: main
  templates:
    - name: main
      steps:
        - - name: hour-loop
            template: print-hour
            arguments:
              parameters:
                - name: hour
                  value: "{{item}}"
            withSequence:
              count: 24
              start: 0
              format: "%02d"
    - name: print-hour
      inputs:
        parameters:
          - name: hour
      container:
        image: busybox
        command: [sh, -c]
        args:
          - |
            echo "Processing hour: {{inputs.parameters.hour}}";

Enter fullscreen mode Exit fullscreen mode

Here’s what’s happening:

  • withSequence generates a sequence from 00 to 23.
  • Each hour is passed to the hour parameter and printed.

Wrapping Up

That’s it! Hope this helps you handle dates in your Argo Workflows more smoothly. Let me know if you try it out or if you have your own tricks to share! 😊

Top comments (0)