Formatting a date into another field

I want to create a contact from an email

I take the date from the email and I put it into the contact notes as “”
I thought I would use an expression like

{{ moment( new Date($json[“headers”][“date”]) ).format(“yyyyMMdd”) }}

But it is giving an error

new Date($json[“headers”][“date”]) works

What should I do?

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

n8n doesn’t use momentjs library, Which is where that syntax is from. N8n uses the luxon Library.
if you’re using ChatGPT specify the luxon library and it should give you the right answer.

How you convert it to a time will depend on what the format is in. If it is in iso format you can do {{ DateTime.fromISO("2016-05-25T09:24:15") }}

if not, you can look through the other parsing methods or use the fromFormat("","") syntax. That would be like {{ DateTime.fromFormat("2016-05-25T09:24:00", "yyyy-MM-dd'T'HH:mm:ss") }}

Then from there you can use .format("") to get any format you like.

Like {{ DateTime.fromFormat("2016-05-25T09:24:00", "yyyy-MM-dd'T'HH:mm:ss").format(“yyyyMMdd”) }}

Hope that helps. If you have trouble parsing the date in the format you have please share it and I’ll help you out

2 Likes

the gmail seems to send

Thu, 12 Oct 2023 22:15:26 +0000

but there’s another node

image

Not sure where this format is from.

But anyway I came up with

{{ DateTime.fromFormat( $json[“headers”][“date”], “ccc, dd LLL yyyy HH:mm:ss Z”).toFormat(“yyyyMMdd”) }}

but it’s invalid.

image

My final goal is that I get gmail sent from different web forms. I create contacts and the notes field should have

“yyyyMMdd” + [form name]

That time shown is actually in ISO format so it’s easy, just use .fromIso("").
If you waned to do it manually. that would be yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

See the picture below

Thanks. Finally got it green


{{ $json.headers.date }}
{{ $json["headers"]["date"].split(': ')[1] }}

{{ DateTime.fromFormat( $json["headers"]["date"].split(': ')[1], "ccc, dd LLL yyyy HH:mm:ss ZZZ") }} 

{{ $json["date"] }}
{{ DateTime.fromFormat( $json["date"], "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").toFormat("yyyyMMdd") }}

Do you know where

{{ $json[“date”] }}

came from? It’s not

$json[“headers”][“date”]

And from what I understand the expression always has to be a single line and I cannot use things to make it easier to read such as intermediate variables.

Is there any way to make this more manageable because I can’t imagine having this in multiple nodes and trying to keep them updated?

Hey @bally,

$json tends to mean the data is coming from the input so this means the node before has a date field in the output which is where it is coming from as to why it is different I am not sure it could be that one is a sent date and one is the local recieved date.

what are thoughts on using a node like this

of course this means everything will have to specify specific nodes

image

image

which also seems a bit messy - especially if I ever want to rename the trigger

the ex programme in me wants to define functions

I don’t really see anything wrong with that although you can just format the date in a node with Luxon so no need to add the extra node.

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