# Converting a date string to UTC (with dayjs?)

**URL:** https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217
**Category:** General
**Created:** [8 June 2026 00:10 UTC](https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217 "2026-06-08T00:10:40Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [8 June 2026 00:10 UTC](https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217/1 "2026-06-08T00:10:40Z")

</div>

This is a snippet from the Octopus API example output for consumption:

```auto
{
      "consumption": 0.078,
      "interval_start": "2023-03-26T00:30:00Z",
      "interval_end": "2023-03-26T02:00:00+01:00"
    },
{...}, 
etc

```

What is the best way to format these two date strings as UTC timestamps 'YYYY-MM-DD HH:MM:SS' for insertion into Mariadb?

I'm struggling to get dayjs.utc available in a function.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [8 June 2026 04:35 UTC](https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217/2 "2026-06-08T04:35:45Z")

</div>

I would hack my way around like:

```auto
function dateToDB(d){
    return new Date(Date.parse(d)).toISOString().replace(".000Z", "").replace("T", " ")
}

msg.payload = dateToDB("2023-03-26T02:00:00+01:00") // "2023-03-26 01:00:00"
return msg;

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [8 June 2026 08:08 UTC](https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217/3 "2026-06-08T08:08:58Z")

</div>

Assuming that it is a datetime field, then something like this may be a good way

```auto
INSERT INTO table (date_col) 
VALUES (STR_TO_DATE('2026-05-10T10:32:01Z', '%Y-%m-%dT%H:%i:%sZ'));   

```

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [8 June 2026 09:37 UTC](https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217/4 "2026-06-08T09:37:13Z")

</div>

Thanks both.

I've gone with @bakman2's answer, but I like the idea of making the database do the work so I will also give @Colin's answer a try too.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [8 June 2026 09:52 UTC](https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217/5 "2026-06-08T09:52:18Z")

</div>

I find it mind boggling that the db will not accept an ISO date string directly.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [8 June 2026 12:12 UTC](https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217/6 "2026-06-08T12:12:25Z")

</div>

Indeed.

A couple of notes:

- If you need to do this conversion a lot, probably a good idea to create a db custom function to do it.
- MariaDB/MySQL store dates only as UTC so don't forget to deal with that.

BTW, SQLite is worse since it has no date type at all. Though it does, at least, have a function to convert time strings. Just note that a trailing timezone is NOT a valid ISO8601 string, though SQLite does support some variations, just not timezones.

It is best practice to store dates and times as UTC anyway.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [8 June 2026 12:30 UTC](https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217/7 "2026-06-08T12:30:44Z")

</div>

> [@TotallyInformation](#):
>
> Just note that a trailing timezone is NOT a valid ISO8601 string

Could you clarify what you mean by that? [Wikipedia](https://en.wikipedia.org/wiki/ISO_8601) suggests that "2026-06-08T10:42:35Z" and "2026-06-07T22:42:35-12:00" are valid.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [8 June 2026 14:37 UTC](https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217/8 "2026-06-08T14:37:50Z")

</div>

> [@TotallyInformation](#):
>
> ISO8601

OK, I over stated it. You are correct that the actual standard does allow for numeric time offsets in +/-HH:MM format, some date/time handling tools do not support that aspect.

Neither MariaDB/MySQL nor SQLite support numeric offsets.

JavaScript only partially supports them. It treats ISO timestamps as UTC only but it can convert inputs with numeric offsets.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [22 June 2026 14:38 UTC](https://discourse.nodered.org/t/converting-a-date-string-to-utc-with-dayjs/101217/9 "2026-06-22T14:38:29Z")

</div>

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