get date from firebase format change - javascript

earlier we push date use native Firebase format
createAt:admin.firestore.Timestamp.fromDate(new Date)
so data store like this
"createAt": {
"_seconds": 1563181560,
"_nanoseconds": 567000000
}
new we change like this
createAt:new Date().toISOString()
now store like this
"createAt": "2019-07-17T07:17:05.115Z"
buts some date store like this because we use native date format
now how can i call old date to new date format? moste of the data sote like native date method is that any way to change new method?

The Firestore Timestamp type has many conversion methods. Just like it has a fromDate() it also has a toDate() method. So if you reload the data from Firestore and get a Timestamp object, you can then get a date with timestamp.toDate() and thus an ISO-8859 formatted string with timestamp.toDate().toISOString().

Related

How to pass a JS date object to cloud function for storing it on Firestore

When I write a Date object to my Firestore, all works correctly and I can see a date in the DB stored with the type "timestamp". If I try to pass a Date object to a cloud function, I don't know why but it is an empty object when I store it from the cloud function to the firestore DB.
firebase.functions().httpsCallable("myFunction")({date: new Date()}) <---- // this doesn't work
So, I have decided to convert the Date object to a firestore timestamp in the client side before sending it to the cloud function. As follows:
date = firebase.firestore.Timestamp.fromDate(date);
firebase.functions().httpsCallable("myFunction")({date})
Then, if I get it in the cloud function and store it on my firestore, I see an object with the fields "seconds" and "nanoseconds", but not a timestamp (I mean it is not stored with the type Timestamp)... So when I get this date I cannot apply the method .toDate()
Is there any good way to store the date in this situation as a timestamp and not as an object?
The input and output of callable functions deal entirely with JSON, so they are limited to expressing only those types that are allowed in JSON: string, number, boolean, null, object, array. Date and Timestamp are not among those types. What the Firebase SDK will do is try to serialize custom objects as JSON, which is not working the way you want - the seconds and nanos fields of the Timestamp are being split up during serialization.
If you have a Date to send from the client, you should just send it as a number. Instead of passing a Date object, pass the single number inside it (milliseconds since unix epoch):
const number = date.getTime()
On the receiving side, in Cloud Functions, take the number and convert it back to a Date:
const date = new Date(number)
Then you can write the Date object to a Firestore document field.

Firebase Firestory query where stored timestamp is in the past

I need to delete all Documents which have a timestamp (stored in a field) prior to today.
The timestamp is created in the Firestore GUI. The following query doesnt return any docs.
collectionRef
.where('timestampFieldName', '<', Date.now())
.get()
What exactly is a timestamp, created in the GUI and how to compare it with any date?
Whenever passing a date to Firestore, you should pass in an actual Date object. Date.now() returns a timestamp, which is just a number and not a Date object itself. To get the actual Date for the same value, use new Date(). So:
collectionRef
.where('timestampFieldName', '<', new Date())
.get()

Date format for sitemap

I'm using gadicohen:sitemaps with Meteor to create my sitemaps and I'm not sure how to set the lastmod date field. The documents state not to use new Date(), so what date format should I use.
{ page: '/x', lastmod: new Date() },
They aren't saying not to use Date as a type, they are saying that you should not create a new Date() every time the sitemap is requested.
Instead, keep in your collection the time that the page was last modified, and use that in the sitemap

How to process data before set or get operation?

I am using primeNG calendar and I got a model called myDate and a dateformat. ngModel directive referencing to a Date property.
<p-calendar [(ngModel)]="myDate" dateFormat="dd/mm/yy"></p-calendar>
But problem is I want to store myDate value as Unix Timestamp. So I need to convert myDate to milliseconds before set and convert it to date object with dateformat before get operation. Is there any way to do this?
private myDate;
setMyDate(myNewDate){
this.myDate = convertDateToTimestamp(myNewDate)
}
getMyDate(){
return convertTimestampToDate(this.myDate)
}
You can call getTime() on this date object to get it in unix form. It comes out in milliseconds.
new Date("2013/09/05 15:34:00").getTime();
It may have decimal bits so wrapping it in Math.round would clean that.
Math.round(new Date("2013/09/05 15:34:00").getTime());
how to convert a string to a Unix timestamp in javascript?
You could use getters and setters to achieve this, which is actually very close to what you already have:
private _myDate;
set myDate(myNewDate){
this._myDate = convertDateToTimestamp(myNewDate)
}
get myDate(){
return convertTimestampToDate(this._myDate)
}

Create timezone-aware date in javascript

When I create objects in my MongoDB using Mongoose, the dates are formatted as, for instance, 2016-10-10T15:35:52.764Z (it might be yyyy-MM-ddTHH:mm:ssZ). If I use Date.now() in JavaScript, I get a timestamp.
How can I create the same date format as Mongoose with javascript?
Edit
It seems I can use
new Date().toISOString();
but what is the difference of, for instance, Date.now() and new Date()?
Are there any reasons for not using new Date() instead of a function under the Date object (that somehow already seems to be initialized without writing new Date()?)
you can use moment.js and run like this
moment().format('YYYY-MM-DDTHH:mm:ss'); // print 2016-10-11T15:25:36

Categories

Resources