Best answer

How to get today's date (Not via zap_meta_human_now)

  • 6 May 2021
  • 6 replies
  • 16219 views

Userlevel 4
Badge +4

I know there is {{zap_meta_human_now}} for adding the date and time the zap runs. 

But I am looking for something like “Get Today’s Date” at the beginning of a new Zap to compare this date with a recently added date with {{zap_meta_human_now}} in a certain field. 

Is this possible with a Javascript or Python script (get current date)?

Thanks for help.

icon

Best answer by SamB 16 March 2022, 17:39

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

6 replies

Userlevel 7
Badge +10

Definitely seems possible via a code step:

https://www.programiz.com/python-programming/datetime/current-time

Userlevel 4
Badge +4

@AndrewJDavison_Luhhu That’s perfect! Thanks a lot. The one problem is, that the output of the current time is wrong (it’s not the correct timezone). Is it possible to choose the correct timezone or just add the missing value to the hour? I.e. 09+2 (2 = the difference to the local timezone).

 

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()

print("now =", now)

# YY-MM-DD HH-mm
dt_string = now.strftime("%Y-%m-%d_%H-%M")
print("date and time =", dt_string)

 

Userlevel 4
Badge +4

I do have a problem now with the output of this Python script: 

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()

print("now =", now)

# YY-MM-DD HH-mm
dt_string = now.strftime("%Y-%m-%d_%H-%M")
print("date and time =", dt_string)

 

The test did work and I got the correct result, but when I want to add the result in a later step of the Zap the output is missing: Please return output or return early.

I am sorry, but I do not know Python so well. What does this mean? 

Thanks for help.

 

Userlevel 7
Badge +12

@michael291

Unless you are doing the comparison in a code step, I don’t see how it would be different than just using a formatter with {{zap_meta_human_now}}

If you just want the date and not the time field you can specify that in the formatter

 

 

Userlevel 4
Badge +4

Thanks @GetUWired you’re right. Didn’t know I can convert the timezone too.

Userlevel 7
Badge +11

Hey all! It seems like @michael291 may have ended up using a Formatter step and {{zap_meta_human_now}} to output the date and time a Zap runs as GetUWired suggested. But I thought I’d follow up here to confirm that it is possible to output the date and time a Zap runs using Python.

For example this Python code:

# imports the datetime module

from datetime import datetime



# Gets the current date and time

time_now = datetime.now()



#converts the default date time into YYYY-MM-DD HH:MM format

date_time = time_now.strftime("%Y-%m-%d %H:%M")



#outputs the current date and time in a field called "Date and Time"

return {

    'Date and Time': date_time

}

would create a field called Date and Time with the current date and time:
93cc74426b1cf4cbb31503e337eba180.png

Which you could then select in subsequent steps:

73f93454d49812245a8cf23c6674ea14.png

And if you wanted to output just the date you could adjust the code so that only the date is output:

# imports the datetime module

from datetime import datetime



# Gets the current date and time

time_now = datetime.now()



#converts the default date time into YYYY-MM-DD format

date_only = time_now.strftime("%Y-%m-%d")



#outputs the current date and time in a field called "Date"

return {

    'Date': date_only

}

Like so:
00acad0b1e42d84a9ce76c6d9654c78b.png

You can find out more about the different date formatting options available in Python here: strftime() and strptime() Format Codes :slight_smile: