# Adding timestamp to email export

**URL:** https://community.tago.io/t/adding-timestamp-to-email-export/1441
**Category:** Analysis and Actions
**Created:** [July 28, 2023, 5:35pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441 "2023-07-28T17:35:22Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![wrselbig](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@wrselbig](https://community.tago.io/u/wrselbig)
#### Post date: [July 28, 2023, 5:35pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/1 "2023-07-28T17:35:22Z")

</div>

The ‘Email export’ analysis provided by Tago is a simple way to automate export of data from a sensor for a defined period of time. However, the export file only contains the value without a timestamp. This makes the data practically useless. Is there a way to modify the analysis so that it adds a timestamp for each value exported?

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [July 28, 2023, 7:28pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/2 "2023-07-28T19:28:57Z")

</div>

Hi,

If you wanna get the timestamp when the data are created you can change this party of code for this:

// Create csv header

let csv = “Fuel Level, Created At”;

// For each record in the fuel\_list, add the value in the csv text.

// Use \n to break the line.

for (const item of fuel\_list) {

csv = `${csv},\n${item.value}, ${item.time}`;

}

Lines 38-44.

---

<div class="post-metadata">

### Author: ![wrselbig](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@wrselbig](https://community.tago.io/u/wrselbig)
#### Post date: [July 28, 2023, 7:34pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/3 "2023-07-28T19:34:21Z")

</div>

Excellent, many thanks!

---

<div class="post-metadata">

### Author: ![gabriel.pupo](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@gabriel.pupo](https://community.tago.io/u/gabriel.pupo)
#### Post date: [August 14, 2023, 8:27pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/4 "2023-08-14T20:27:08Z")

</div>

Hi guys,

In this analysis did you get two columns with different variable in the same csv file?  
I got it but with both the two variables in the same column.

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [August 15, 2023, 8:42pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/5 "2023-08-15T20:42:29Z")

</div>

Hi Gabriel,

Yes, each value is separated by distinct columns.

 ![](https://canada1.discourse-cdn.com/flex034/uploads/tagoio1/original/2X/f/fcff2dae1e0dfba000f0a45a516acdd624dc5f89.png)

---

<div class="post-metadata">

### Author: ![gabriel.pupo](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@gabriel.pupo](https://community.tago.io/u/gabriel.pupo)
#### Post date: [August 15, 2023, 10:16pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/6 "2023-08-15T22:16:35Z")

</div>

Hi Mateus, thanks for reply

But I mean, two diferentes variables. Like:

 ![](https://canada1.discourse-cdn.com/flex034/uploads/tagoio1/original/2X/c/cc91460094f7d172fd0ef1de506cd5e7146abd04.png)

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [August 16, 2023, 12:47pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/7 "2023-08-16T12:47:29Z")

</div>

Hi,

Do you want each variable to generate a new CSV file, or keep the variable values in the same column within the CSV? I didn’t understand what you want to do.

---

<div class="post-metadata">

### Author: ![gabriel.pupo](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@gabriel.pupo](https://community.tago.io/u/gabriel.pupo)
#### Post date: [August 16, 2023, 8:16pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/8 "2023-08-16T20:16:57Z")

</div>

I want two variables in the same CSV file, but in diferent columm. Like in the photo

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [August 17, 2023, 6:40pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/9 "2023-08-17T18:40:02Z")

</div>

I gotcha, you can do that, change lines **36 to 45** and put this code.

const items = await device.getData({ variable: [“fuel\_level”, “fuel\_temperature”], qty: 5 });

const result = items.reduce((acc, item) =\> {

if (item.variable === “fuel\_level” || item.variable === “fuel\_temperature”) {

if (!acc[item.variable]) {

acc[item.variable] = ;

}

acc[item.variable].push(item.value);

}

return acc;

}, {});

// Create csv header

let csv = “Fuel Level, Fuel Temperature, Created At”;

for (let i = 0; i \< result.fuel\_level.length; i++) {

csv += `\n${result.fuel_level[i]}, ${result.fuel_temperature[i]}, ${items[i].time}`;

}

---

<div class="post-metadata">

### Author: ![gabriel.pupo](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@gabriel.pupo](https://community.tago.io/u/gabriel.pupo)
#### Post date: [August 18, 2023, 1:33am UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/10 "2023-08-18T01:33:33Z")

</div>

Thank you Mateus, this worked! You helped me a lot!

And another doubt. I want use this code more funtional.

Do you know how to take the data from a period, like one month?

Because to day in this code you define the quantity you want of the last data.

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [August 18, 2023, 1:05pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/11 "2023-08-18T13:05:32Z")

</div>

Gabriel, please refer to the documentation for the available parameters that can be used with the “sendData” method. You can find them here:

[https://js.sdk.tago.io/classes/Device.html#getData](https://js.sdk.tago.io/classes/Device.html#getData)

Click on “DataQuery” to view the applicable parameters.

---

<div class="post-metadata">

### Author: ![wrselbig](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@wrselbig](https://community.tago.io/u/wrselbig)
#### Post date: [January 29, 2024, 3:55pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/12 "2024-01-29T15:55:07Z")

</div>

This is a great script! Is there a way to output the time stamp in a specific format? It’s unusable in it’s current form of “Mon Jan 29 2024 15:50:11 GMT+0000 (Coordinated Universal Time)”. How would one format the timestamp to something like MM-DD-YYYY HH:MM?

---

<div class="post-metadata">

### Author: ![mateus.silva](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mateus.silva](https://community.tago.io/u/mateus.silva)
#### Post date: [January 29, 2024, 8:10pm UTC](https://community.tago.io/t/adding-timestamp-to-email-export/1441/13 "2024-01-29T20:10:37Z")

</div>

For more complex date formatting, you might want to use a library like date-fns or moment.js. These libraries provide a lot of utility functions for working with dates.

Here’s an example of how you can format a date using date-fns:

```auto
import { format } from 'date-fns';

const date = new Date();
const formattedDate = format(date, 'MM-dd-yyyy HH:mm');

console.log(formattedDate); // Outputs date in the format "MM-dd-yyyy HH:mm"

```

And here’s an example using moment.js:

```auto
import moment from 'moment';

const date = moment();
const formattedDate = date.format('MM-DD-YYYY HH:mm');

console.log(formattedDate); // Outputs date in the format "MM-DD-YYYY HH:mm"

```

Remember to install these libraries using npm before using them.
