Skip to content

Instantly share code, notes, and snippets.

@kathawala
Created April 21, 2020 22:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kathawala/278676b4b80eb4d1f344dbcd84192375 to your computer and use it in GitHub Desktop.
Save kathawala/278676b4b80eb4d1f344dbcd84192375 to your computer and use it in GitHub Desktop.
Converts a python datetime to a crontab expression which fires once (at the date+time specified in the python datetime object)
from datetime import datetime
def datetime_to_cron(dt):
# FYI: not all cron implementations accept the final parameter (year)
return f"cron({dt.minute} {dt.hour} {dt.day} {dt.month} ? {dt.year})"
@matheusjiranrock
Copy link

Had a problem with datetime.strftime running on Windows. It ran well on Linux, but in Windows it crashed with "Invalid String Format" error.
This fixed it, thank you very much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment