Comparing TimeDate strings (what format is this?)

How do I figure out what format of timedate string I have to compare?

I receive an authorization token for a process with a 24 hour timeout. Regenerating the token can cause failures in my workflow. So, I want to write the token and the expiration date to a file local to my n8n instance. Generating and writing are all working well. I can also read the file.
The token has a 24hour lifespan. So logically, I should be able to read the token from the file, check the expiration date, return to the calling flow if it has not expired, or regenerate, store, then return the updated token if the first is expired.

My issue is the date format. What I get is ‘Sun, 04 Feb 2024 00:34:27 GMT’. The error message indicates it’s not recognized.

None or “ERROR: The provided value 2 ‘Sun, 04 Feb 2024 00:34:27 GMT’ in condition 1 cannot be converted to the expected type ‘dateTime’ [item 0]”

Please share your workflow

Share the output returned by the last node

[
 {
  "data": 
  [
   {
    "kgApiAuthBearer": 
    "Bearer 
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MDY5MjI5NDYsImV4cCI6MTcwNzAwOTM0NiwiaXNzIjoiTW9zeWxlIiwiZGF0YSI6eyJVc2VySUQiOiIzODQyMTE4IiwiZW1haWwiOiJydXNzZWxsQGtnaW5nZXIuY29tIn19.eToA0CXLzxl61xLyzRktyVpulNQMg8JIPipcvHqxIfc",
    "kgApiAuthExpires": "Sun, 04 Feb 2024 01:15:46 GMT"
   }
  ]
 }
]

Information on your n8n setup

  • n8n version: 1.26.0
  • Database (default: SQLite): PostgresQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker in Digital Ocean
  • Operating system: Ubuntu 22.04.3 LTS

You could use a Date & Time Node like this to format both dates before comparing.

If you wanted to do it with code, you can convert that String value to a date value with this {{ DateTime.fromFormat("Sun, 04 Feb 2024 00:34:27 GMT", "EEE, dd MMM yyyy HH:mm:ss z") }}

Edit: i thought you just needed to convert it to date but it needs to be in ISO format, so add the method .toISO() and then it will work in the if statement.
{{ DateTime.fromFormat("Sun, 04 Feb 2024 00:34:27 GMT", "EEE, dd MMM yyyy HH:mm:ss z").toISO() }}

The weird thing is i find it only returns in the default timezone, so if i am dealing with timezones different than mine i append them manually.

You can format it like this {{ DateTime.fromFormat("Sun, 04 Feb 2024 00:34:27 GMT", "EEE, dd MMM yyyy HH:mm:ss z").format("D'T'TT") }} which will return 2/3/2024T19:34:27

Hope that helps

2 Likes

Thank you so much. That was enough information to get me past that issue. In the end, I converted both dates to unix timestamp and compared them. Testing seems to prove it’s working.

-Russ

1 Like

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