20

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.

Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
Jim Bistis
  • 281
  • 1
  • 2
  • 9
  • Can you provide some sample code? – Jason Berryman Feb 13 '18 at 16:18
  • Did you find the way to do it? – Yossi Mar 17 '18 at 08:13
  • If you use the Firestore Console to create a document and select `timestamp` as a field type, it will save as a ISO8601 string. Also, a document's `createTime` and `updateTime` parameters are also stored in this format. So, unless you have a very specific requirement to store that date as a Linux/Unix timestamp, I'd suggest sticking with your current format. – Jason Berryman Mar 25 '18 at 21:16

2 Answers2

17

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

Aravin
  • 6,605
  • 5
  • 42
  • 58
3

You should use the firestore function firebase.firestore.Timestamp.fromDate as below

const mydoc = {
  name: 'John Doe',
  createdAt: firebase.firestore.Timestamp.fromDate(new Date("December 10, 1815")) 
}

// save the document 
db.collection("mycollection").doc("id1").set(mydoc).then(function() {
    console.log("Document successfully written!");
});
Chung Nguyen
  • 481
  • 4
  • 6