Logo

dev-resources.site

for different kinds of informations.

Handling Dates in Argo Workflows

Published at
1/13/2025
Categories
dataengineering
tutorial
argoworkflows
Author
olgabraginskaya
Author
15 person written this
olgabraginskaya
open
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! 😊

dataengineering Article's
30 articles in total
Favicon
Handling Dates in Argo Workflows
Favicon
Massively Scalable Processing & Massively Parallel Processing
Favicon
Pandas + NBB data 🐼🏀
Favicon
Data Engineering Foundations: A Hands-On Guide
Favicon
When to use Apache Xtable or Delta Lake Uniform for Data Lakehouse Interoperability
Favicon
Using Apache Parquet to Optimize Data Handling in a Real-Time Ad Exchange Platform
Favicon
The Columnar Approach: A Deep Dive into Efficient Data Storage for Analytics 🚀
Favicon
Optimizing Data Pipelines for Fiix Dating App
Favicon
What kind of Data Team should I join?
Favicon
Tech Interviews: The Hustle Behind Tech Interview Prep
Favicon
New article alert! Data Engineering with Scala: mastering data processing with Apache Flink and Pub/Sub ❤️‍🔥
Favicon
Hire Big Data Developers for Scalable Solutions
Favicon
Why Feature Scaling Should Be Done After Splitting Your Dataset into Training and Test Sets
Favicon
How Data Analytics in the Cloud Can Level Up Your App
Favicon
Exploring OSM changesets via DuckDB
Favicon
Unlocking the Potential of the JOI Database
Favicon
I built a data pipeline tool in Go
Favicon
Data engineer, plsql
Favicon
Data Warehousing Architectures
Favicon
Cultivating a Data-Centric Culture at Work
Favicon
How Genius Sports slashed costs and lowered latencies for last-mile data delivery
Favicon
Read, Like & Share
Favicon
Surge Datalab Private Limited
Favicon
🤯 #NODES24: a practical path to Cloud-Native Knowledge Graph Automation & AI Agents
Favicon
Can AI finally generate best practice code? I think so.
Favicon
How to Prevent Duplication in Data Aggregation with BladePipe
Favicon
How to Migrate Massive Data in Record Time—Without a Single Minute of Downtime 🕑
Favicon
aMarketForce: Premier Contact List Development & Data Solutions
Favicon
Image processing in JAVA
Favicon
Data Engineering Essentials for E-commerce from ETL to Real-Time Analytics

Featured ones: