Post date conversion in instantsearch


I am trying to convert a timestamp 1514234382 for a post to the formats 2018-01-25 and 25. January 2018 and if it were in PHP I would have fixed this easy but in JavaScript it is a little more work.

Is there a way to convert the date timestamp from Algolia to the formats I need?

This code snippet works for format 1 except it takes the date of today. How do I push in the timestamp (1514234382) to it?
var testdate = new Date().toISOString().slice(0,10);

Best regards.

Hi @damir,

In JavaScipt the Date constructor accepts multiple types of values, one of them is number. The constructor accept a timestamp in milliseconds and not seconds. The one that you provide is in seconds so you need to multiply your value by 1000.

const timestampInSeconds = 1514234382;
const timestampInMilliseconds = timestampInSeconds * 1000;

const date = new Date(timestampInMilliseconds);
const dateFormatted = date.toISOString().slice(0,10);

That did the trick! Milliseconds was the issue! I have learned something new here! Thank you @samuel.vaillant! :smiley:

Yes me too actually! I didn’t know about the milliseconds argument :slightly_smiling_face:

1 Like