How do i convert firestore timestamp to date into different format en c#

Viewed 1558

I'm trying to put a timestamp in a dateTimePicker value in c #, I don't know if it's possible, but I'm getting an error. if is possible, I would like to put it in the format ("dd/MM/yyyy").

dateTimeDate.Value = student.date;

but i get this error,

Cannot implicitly pass the type "Google.Cloud.firestore.Timestamp" in "System.DateTime". 

So i try this, but still not working.

dateTimeDate.Value = student.date.ToDateTime();

How can i fix this, and also i would like to know how to convert it to string to put in a textBox.

2 Answers

Firestore returns Google.Cloud.Firestore.Timestamp object If we convert it to string seems like:

Timestamp: 2021-04-03T10:34:48.865466Z

We can convert it to DateTime object:

DateTime result = DateTime.ParseExact(TIMESTAMP.ToString().Replace("Timestamp:", "").Trim(), 
    "yyyy-MM-ddTHH:mm:ss.ffffffK", null);

In C#, you can cast the object you get back from Firestore to a Firebase.Firestore.Timestamp. Then you will get access to the <Timestamp>.ToDateTime() method.

using Firebase.Firestore;

// code to load data from Firestore
// snapshot is the result of docReference.GetSnapshotAsync()

var dict = snapshot.ToDictionary();
var timestamp = (Timestamp)dict["myTimestampFieldName"];
var myDateTime = timestamp.ToDateTime();
Related