# I am trying to convert unix timestamp(string) coming from my dynamo db by using lambda function to date format to visualize in kibana

**URL:** <https://discuss.elastic.co/t/i-am-trying-to-convert-unix-timestamp-string-coming-from-my-dynamo-db-by-using-lambda-function-to-date-format-to-visualize-in-kibana/303622>\
**Category:** Elasticsearch\
**Created:** [April 29, 2022, 4:35pm UTC](https://discuss.elastic.co/t/i-am-trying-to-convert-unix-timestamp-string-coming-from-my-dynamo-db-by-using-lambda-function-to-date-format-to-visualize-in-kibana/303622 "2022-04-29T16:35:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kedhar1006](https://avatars.discourse-cdn.com/v4/letter/k/e68b1a/32.png) [@Kedhar1006](https://discuss.elastic.co/u/Kedhar1006)\
**Post date:** [April 29, 2022, 4:35pm UTC](https://discuss.elastic.co/t/i-am-trying-to-convert-unix-timestamp-string-coming-from-my-dynamo-db-by-using-lambda-function-to-date-format-to-visualize-in-kibana/303622/1 "2022-04-29T16:35:18Z")

</div>

'use strict';

var moment = require('moment');

//var Elasticsearch = require('Elasticsearch');

//var client = new Elasticsearch.Clinet({

// host: 'https://.us-east-2.es.amazonaws.com',

// log: 'error'

//});

const { Client } = require('Elasticsearch')

const client = new Client({

node: 'https://.us-east-2.es.amazonaws.com',

log: 'error'

})

module.exports.es = async (event, context) =\> {

for (var i = 0; i \< event.Records.length; i++) {

var record = event.Records[i];

try {

```
 if (record.eventName == "INSERT") {

```

// const date = moment(record.dynamodb.NewImage?.timestampResult?.N, 'X').format('yyyyMMdd'T'HHmmss.SSSZ')

```
    let date

     try{

          date = new Date(record.dynamodb.NewImage?.timestampResult?.N * 1000).toISOString()

        }

      catch(e)

      {

          date = record.dynamodb.NewImage?.timestampResult?.N

      }

   var result = await client.create({

     index: 'uuidtestkit-index',

     type: 'Testkits-test',

```

// id: 'uuidTestkit',

```
     id: record.dynamodb.NewImage.uuidTestkit.S,

     body: {

      uuidTestkit: record.dynamodb.NewImage.uuidTestkit.S,

      timestampAgent: record.dynamodb.NewImage?.timestampAgent?.N,

      timestampResult: date,

      testResult: record.dynamodb.NewImage?.testResult?.S,

      timestampUser: record.dynamodb.NewImage?.timestampUser?.N,

      txLink: record.dynamodb.NewImage?.txLink?.S,

      profile_name: record.dynamodb.NewImage?.profile_name?.S,

      profile_picture: record.dynamodb.NewImage?.profile_picture?.S,

      profile_clientID: record.dynamodb.NewImage?.profile_clientID?.S,

      profile_lastname: record.dynamodb.NewImage?.profile_lastname?.S,

      Leaves: record.dynamodb.NewImage?.Leaves?.S,

      userId: record.dynamodb.NewImage?.userId?.S,

      urlVpToken : record.dynamodb.NewImage?.urlVpToken?.S,

      VcToken: record.dynamodb.NewImage?.VcToken?.S,

      VpToken: record.dynamodb.NewImage?.VpToken?.S,

      agentId: record.dynamodb.NewImage?.agentId?.S,

     }

   });

   console.log("=== completed ===");

   console.log(result);

 }

```

}

catch (err) {

```
 console.log(err);

```

}

}

return 'Successfully processed: ${event.Records.length} records.';

};

I am using above node js code for lambda function.  
I can able to convert the unix timestamp to date. But it is still converting as a string and not allowing me to use data range or date histogram options in kibana.

Any help can be appreciated.

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [April 29, 2022, 5:20pm UTC](https://discuss.elastic.co/t/i-am-trying-to-convert-unix-timestamp-string-coming-from-my-dynamo-db-by-using-lambda-function-to-date-format-to-visualize-in-kibana/303622/2 "2022-04-29T17:20:11Z")

</div>

What is your [mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html) for that field in that index? It needs to be a `date` if you want to perform functions in Kibana as a date.

---

<div class="post-metadata">

**Author:** ![Kedhar1006](https://avatars.discourse-cdn.com/v4/letter/k/e68b1a/32.png) [@Kedhar1006](https://discuss.elastic.co/u/Kedhar1006)\
**Post date:** [May 2, 2022, 2:31pm UTC](https://discuss.elastic.co/t/i-am-trying-to-convert-unix-timestamp-string-coming-from-my-dynamo-db-by-using-lambda-function-to-date-format-to-visualize-in-kibana/303622/3 "2022-05-02T14:31:41Z")

</div>

I am not sure how to give format as date in my above code.

But i tried try{

```
      date = new Date(record.dynamodb.NewImage?.timestampResult?.N * 1000).toISOString()

    }

  catch(e)

  {

      date = record.dynamodb.NewImage?.timestampResult?.N

  }

```

and in body i gave .D as well for respective field.

---

<div class="post-metadata">

**Author:** ![Kedhar1006](https://avatars.discourse-cdn.com/v4/letter/k/e68b1a/32.png) [@Kedhar1006](https://discuss.elastic.co/u/Kedhar1006)\
**Post date:** [May 2, 2022, 2:35pm UTC](https://discuss.elastic.co/t/i-am-trying-to-convert-unix-timestamp-string-coming-from-my-dynamo-db-by-using-lambda-function-to-date-format-to-visualize-in-kibana/303622/4 "2022-05-02T14:35:44Z")

</div>

And even if i pass a number it is somehow converting as a string in kibana.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 30, 2022, 2:36pm UTC](https://discuss.elastic.co/t/i-am-trying-to-convert-unix-timestamp-string-coming-from-my-dynamo-db-by-using-lambda-function-to-date-format-to-visualize-in-kibana/303622/5 "2022-05-30T14:36:10Z")

</div>

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