Convert timestamp from GMT to Brazil -3h timezone

Using a Google Sheet node, i extract records with timestamps in the following format:

2022-12-12 22:05:26

The timezone is GMT, and I need to send this time using GMT-3 (Brazil), in this case: 2022-12-12 19:05:26, in an HTTP node:

Any sugestion on how to do that?

Information on your n8n setup

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

Hey @fxholl,

It sounds like something you could do using Luxon if you just need to remove 3 hours, check out the docs here: Handling dates | n8n Docs

The quick version would be something like {{ $json.date.minus({hours: 3}) }}

Hey @Jon @fxholl
it’s not that easy because n8n is not passing the data as luxon date object but instead as string.
You need to convert the string first and then change the timezone. I chose Buenos Aires, but feel free to change it to the capital next to you. Afterwards we need to format the date back to string with correct format.

@fxholl you can just copy the Set node from my workflow, put it before the send to komercial node and then you have the gmt-3 date within the usable attributes.

This is the needed code:

{{ DateTime.fromFormat($json['Submitted At'], 'yyyy-MM-dd HH:mm:ss', { zone: 'utc' }).setZone('America/Argentina/Buenos_Aires').toFormat('yyyy-MM-dd HH:mm:ss') }}

And here you find the formatted date.

Hope that helps.

Cheers

2 Likes

The SET node works beautifully. I adjusted the formula to Brasil/Sao_Paolo:

{{ DateTime.fromFormat($json['Submitted at'], 'yyyy-MM-dd HH:mm:ss', { zone: 'utc' }).setZone('America/Sao_Paulo').toFormat('yyyy-MM-dd HH:mm:ss')}}

However, I now have a problem in the following HTTPS node: when I use the BR field, it inserts a line break and the node returns an error:

Here is the flow:

1 Like

Hey @fxholl
that is a crazy behaviour and I can not reproduce it on my own.
A .trim() could help here.

...
  "sales_date": "{{ $json["sales_date"].trim() }}",
...

Hope that helps. Eben if this should not happen at all.

Cheers

2 Likes

trim function did the trick!!

thank you so much for the help!!

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