Working with timestamps

Hi All,

In my database, I have a timestamp for the record creation which is stored as and passed to weweb as a unix timestamp. What is the best way to convert it to dd/mm/yyyy?

2 Likes

I would convert it in the backend. What backend are you using?

1 Like

Hey Edward, Thanks for replying. I am using Xano.

1 Like

when you get the data from the database, create a variable in Xano that contains the date and format it. Then you can add it to the response at the end and return it together

2 Likes

That’s a great shout. Thanks @Edward

2 Likes

In case, here is a quick video as the format can be a bit tricky. Its d/m/Y for UK format (11/06/2022)

video

2 Likes

Personaly, I wouldn’t do the conversion to a string in the backend.

Timestamp are probably easier to handle for sorting and other calculation (add days or years, calculate time differences,…). And if, at some point, you need to format the date in different format accross you app, having a formatted string to start with will not help.

I would rather avise to create a custom formula that converts the timestamp to a date.

This require a bit of javascript, but nothing too complex I’d think - although there are a few step for the actual conversion (timestamp to milliseconds, milliseconds to Date, Date to formatted string).

Here is a step by step tutorial : Convert a Unix Timestamp to a Date in Vanilla JavaScript


Now, if the webweb teams is reading this : a NoCode formula to format date would be great. don’t you think ?

2 Likes

Yep, working on it :grinning:

4 Likes

@Joyce Is there any news on this? I’m struggling to convert a timestamp stored in xano to display as the initial value in the date picker. I have added the day.js extension, but still can’t get there. Please help!

If you have a timestamp you just need to convert it to a date object and then a string. For example: new Date(1670431416145).toISOString()

EDIT: updated code to convert the timestamp into string.

2 Likes

When you add the date extension, you get access to a bunch of no-code date formulas in the formulas tab.

Here’s an example formatting a timestamp:

2 Likes

That’s super cool :star_struck:
Even better because I’m stumbling on that problem and the solution in 21 hours old :partying_face:

1 Like

@Joyce What do you mean by date extension? I placed a date time picker on the page, but can’t see the date formulas.

Sorry, that wasn’t clear. I mean you need to add the Date plugin:

We don’t include it by default because not every project needs to work with dates. When you add the Date plugin, it adds the Dayjs library to the project but we don’t want to add the weight of the library on projects that don’t need it.

Once you add that plugin, you should be able to see the Date formulas in the usual formula tab in a dedicated dropdown.

2 Likes

Hi @Joyce , I noticed something weird when using the format date formula. Trying to format a timestamp directly gives the correct date without an issue, but trying to format the same timestamp value in a variable gives a totally different result. Is there something I’m missing?

I think that value is coming in as a string. Use the formula toNumber around it.

2 Likes

That makes sense! Thank you @raydeck

2 Likes

@Joyce request to add this info about the Date extension to the docs :pray:t3:

1 Like

Noted!

Hi @Joyce,

How should a work around null values from Xano (timestamp)?

image

I tried using IF and IFEMPTY but nothing works. This expression works when item.data.value is not null