4

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
sebas9981
  • 51
  • 1
  • 2
  • seems the type of dateTimeDate and student.date is different.you can goto the definition to find out – FLYFLU Mar 03 '21 at 00:31
  • 1
    Did you get any error when used `student.date.ToDateTime();` ? – Chetan Mar 03 '21 at 00:46
  • no, but I don't get the date that is in firestore. for some reason, when I use student.date.ToDateTime () I always get 01/01/1970. – sebas9981 Mar 03 '21 at 00:51
  • Not quite sure why you are using a data type that is not listed as a supported data type for firestore https://cloud.google.com/firestore/docs/concepts/data-types. You may want to use Date and Time instead. You would have to do a lot of conversion work to turn a google.cloud.firestore.Timestamp into .net DateTime because they use completely different epoch values. – Dave Holden Mar 03 '21 at 00:53
  • Are you sure you are getting correct date value in `student.date` ? – Chetan Mar 03 '21 at 00:57
  • you're right, I had an error and that's why it doesn't get the correct date. student.date.ToDateTime() it works, I solved it. thank you all and sorry for my english jejje, my native language is spanish. – sebas9981 Mar 03 '21 at 01:15
  • 1
    @sebas9981 Welcome to StackOverflow, can you provide the solution you came up with as an answer to you question and accept it? This will make it easier for other members of the community refer to it in case they go though the same issue, also this will help impoving your reputation on StackOverflow, if you have any doubts on how to do it you can follow [these](https://stackoverflow.com/help/how-to-answer) and [these](https://stackoverflow.com/help/someone-answers) instructions. – Ralemos Mar 04 '21 at 12:45
  • it's easy like ((Timestamp) timeSpanValue ).ToDateTime() – Phd. Burak Öztürk Jun 24 '22 at 21:11

2 Answers2

3

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);
Syscall
  • 19,327
  • 10
  • 37
  • 52
Akash Shah
  • 43
  • 4
  • 2
    There are already methods that allow you to convert to.a DateTime. The Google.Cloud.Firestore.Timestamp type comes with a pre-made method .ToDateTime() which when run on a Timestamp object, will return a DateTime object – Matthew Swallow Apr 29 '21 at 16:57
2

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();