Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a date value in firestore

type="date" in html saves as a string in firestore.

In the html file.

<input #scheduledStartDate="ngModel" [(ngModel)]="reposition.scheduledStartDate"
name="scheduledStartDate" id="scheduledStartDate" class="input is-normal"
type="date" required placeholder="Leaving...">

from the interface .ts file

scheduledStartDate?: DateTimeFormat;

Also tried

scheduledStartDate?: Date;

With the same result.

Both options save the inputted date value as a string in firestore e.g. "2018-01-02" instead of a timestamp.

like image 549
Jim Bistis Avatar asked Feb 13 '18 00:02

Jim Bistis


People also ask

Can you store objects in firestore?

Cloud Firestore is optimized for storing large collections of small documents. All documents must be stored in collections. Documents can contain subcollections and nested objects, both of which can include primitive fields like strings or complex objects like lists.

How do I update my firestore data?

Firestore Update Entire DocumentgetDatabase() → where we want to update a document. doc() → where we'll be passing references of database, collection and ID of a document that we want to update. setDoc() → where we actually pass new data that we want to replace along with the doc() method.


1 Answers

Before saving the javascript object to firestore, just create an instance of Date() and pass the value like this new Date("December 10, 1815")

Syntax

var docData = {
    stringExample: "Hello world!",
    booleanExample: true,
    numberExample: 3.14159265,
    dateExample: new Date("December 10, 1815"),
    arrayExample: [5, true, "hello"],
    nullExample: null,
    objectExample: {
        a: 5,
        b: {
            nested: "foo"
        }
    }
};
db.collection("data").doc("one").set(docData).then(function() {
    console.log("Document successfully written!");
})

Cloud Firestore supports,

Date and time - Chronological - When stored in Cloud Firestore, precise only to microseconds; any additional precision is rounded down.

Note

When a query involves a field with values of mixed types, Cloud Firestore uses a deterministic ordering based on the internal representations.

Official Links

https://firebase.google.com/docs/firestore/manage-data/data-types https://firebase.google.com/docs/firestore/manage-data/add-data

like image 51
Aravin Avatar answered Oct 07 '22 03:10

Aravin