简体   繁体   English

我如何将 Firestore 时间戳转换为不同的格式 en c#

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

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.我正在尝试在 c # 中的 dateTimePicker 值中添加时间戳,我不知道这是否可能,但出现错误。 if is possible, I would like to put it in the format ("dd/MM/yyyy").如果可能的话,我想把它放在格式(“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.我该如何解决这个问题,我也想知道如何将其转换为字符串以放入文本框。

Firestore returns Google.Cloud.Firestore.Timestamp object If we convert it to string seems like: Firestore 返回Google.Cloud.Firestore.Timestamp object 如果我们将其转换为字符串,则如下所示:

Timestamp: 2021-04-03T10:34:48.865466Z时间戳:2021-04-03T10:34:48.865466Z

We can convert it to DateTime object:我们可以将其转换为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 .在 C# 中,您可以将从 Firestore 返回的 object 转换为Firebase.Firestore.Timestamp Then you will get access to the <Timestamp>.ToDateTime() method.然后您将可以访问<Timestamp>.ToDateTime()方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM