How can I get current time in a python code node in n8n v2.x?

I was told by the AI documentation that I could get current time in a python code node with str(_now) only to be told at run time that _now does not exist. And we cannot import datetime. So I have to add a javascript node before that to get current time? come on.

Oh and this does not work for cloud javascript…

const { DateTime } = require(‘luxon’);
const utc_now = DateTime.utc().toISO();

But this does:

const currentTime = new Date().toISOString();

Still want to know why python is being hamstrung to such an extent…

You’re not doing anything wrong this is a documentation + environment mismatch, and it’s understandably frustrating.

What’s actually happening:

  1. _now does not exist in the Python Code node
    That variable is only available in expression context, not inside Code nodes. The docs blur this distinction.

  2. Python Code node is intentionally sandboxed
    On n8n (especially cloud):

    • You cannot import datetime

    • You cannot rely on system time
      This is by design for security and determinism.

  3. Cloud JavaScript does NOT allow require()
    So this fails on n8n.cloud:

[email protected]

The documentation seems to be lonely and take me down a path (with it) that needs undoing. The alternate to luxon does work. Not sure why the documentation did supply that answer rather than the luxon since it works likely for all instances… self-hosted or cloud.

1 Like

Yep, that’s expected on n8n Cloud require() isn’t available there, which is why luxon fails while new Date().toISOString() works.

If you want, I can help you clean this up properly so you’re not fighting the environment:

  • Use n8n-native time helpers ($now) where they make sense

  • Pass timestamps into Code nodes cleanly

  • Keep the workflow consistent between Cloud and self-hosted

Happy to walk through it with you or help refactor the workflow so time handling “just works.”

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.