Working with javascript timestamps in AB/Firestore


Peter Bradstreet
Hi David,

I am working with a todo list in Firebase/Firestore, each item in the todo list may have a due date field. I am using the new datepicker (from DateInput2 sample) to set each due date in the chore add view.

Firestore stores date data as a javascript timestamp object with two properties: seconds and nanoseconds. If I take a look at the Firestore console I can see each 'DueDate' i.e. '28 August 2019 at 00:00:00 UTC-4', This was just selected as a data in AB but converted itself to a timestamp when pushed to Firestore.

If I use the following code in the report after running my query in the grid view

then the following data is displayed for the DueDate: 'Due Date: Timestamp(seconds=1566878400, nanoseconds=0)' however, I am not interested in time, only the data so I would like to display '28 August 2019' in the chores report (grid view) and also place this date into the date picker on the edit view. Is there a way to convert the timestamp to a date field on retrieval so that I can work with it or is there a better approach?

Thanks! Pete

DecSoft

Hello Peter,

About how to show the date into the Report, we can use the "date" filter of AngularJS (read here their documentation), so we can shown the date using a code like the below one inside the Report's HTML:

We are asuming that "MyDate" is a Timestamp, but, can also be other kind of dates. You can modify the format to be used, of course (see the date filter available formats).

And about the DateInput, talking about the sample that you refer, certainly using the Timestamp apparently works as expected, that is, we can replace certain line of the sample by other one, which uses the Timestamp:



Peter Bradstreet
I tried putting this into the report html:
(click to edit)
Chore: [Record.Chore]
Due Date:{{Record.DueDate | date:'longDate' }}
[Record.Details]
When I preview it I still get "Due Date:Timestamp(seconds=1567137600, nanoseconds=0)"

DecSoft

Hello Peter,

It's rare... I try with an string instead of an integer timestamp... and works as expected anyway. It's rare that you get exactly the same result, so, please, manually remove the "_Compiled" directory and also the "_Cache" one, and try to compile and run the app again...



Peter Bradstreet
So it appears that I need to convert the Firestore Timestamp to a JS date format by adding '.toDate' in the pipe.
Due Date: {{Record.DueDate.toDate() | date : "longDate"}}, this code successfully converted it to a js date and then formatted it. Cheers, Pete

DecSoft

Hello Peter,

It's possible... if I am not wrong (I am not an expert) there are different kind of timestamps... so maybe what you used need to be converted to a date before the filter date can play with it. I try with a Javascript timestamp and they work as expected, passing it as a number or as a string.



Peter Bradstreet

So I have a chores view which has a report in it and the following code is displaying the results properly due to the use of ".toDate()" which is an Angular function (See https://stackoverflow.com/questions/52247445/how-do-i-convert-a-firestore-date-timestamp-to-a-js-date)

When the user clicks on a report row I take them to an edit view with the various fields populated including the due date if populated. I know that I receive the due date from Firestore as a Firestore timestamp and I need to convert that to a javascript date field. This is done by using the built in function "DateToJSDate", in my case I defined it as 'ReminderDate' then I added 'window.App.RootScope.datePickerValue = window.App.RootScope.ReminderDate;' to the js code and everything works well.

Cheers, Pete

DecSoft

Hello Peter,

Thanks for sharing, sir. Certainly, it's a question to deal with the dates variables and their different formats, etc.


Everybody can read the DecSoft support forum for learning purposes, however only DecSoft customers can post new threads. Purchase one or more licenses of some DecSoft products in order to give this and other benefits.

This website uses some useful cookies to store your preferences.

I agree. Hide this note. Give me more information.