gpt4 book ai didi

c# - 将 Gmail 日期转换为 Gmail 帐户中显示的日期时间

转载 作者:行者123 更新时间:2023-11-30 14:46:51 25 4
gpt4 key购买 nike

我在我的 c# ASP.net 应用程序中使用 Gmail API 获取收件箱消息。所有消息都来自不同的时区(州/国家/地区),我想在我自己的时区显示日期,因为它在 Gmail 应用程序中显示。我搜索了很多时区之间的转换,但无法解决,可能是我没有正确理解。

我的代码,但我到目前为止尝试过的是:(它适用于大多数消息,但对于其中少数消息,它没有显示正确的日期时间)

例如:

Time displayed in Gmail App: 30/11/2017 2:11 AM

My code Input/ value from Gmail API: Wed, 29 Nov 2017 10:11:35 -0800

以下是我从当前的 Gmail 收件箱邮件中获得的一些值:

enter image description here

 var re = service.Users.Messages.List("me");
re.LabelIds = "INBOX";
re.Q = "is:all";
var res = re.Execute();
if (res != null && res.Messages != null)
{
foreach (var email in res.Messages)
{
var emailInfoResponse = service.Users.Messages.Get("me", email.Id).Execute();
if (emailInfoResponse != null)
{
foreach (var mParts in emailInfoResponse.Payload.Headers)
{
if (mParts.Name == "Date")
{
date = mParts.Value;
}
}
}
}
}

My Time Zone is: (UTC+08:00) Perth

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

{
"id": string,
"threadId": string,
....
"internalDate": long,
"payload": {
....

internalDate long
The internal message creation timestamp (epoch ms), which determines ordering in the inbox. For normal SMTP-received email, this represents the time the message was originally accepted by Google, which is more reliable than the Date header. However, for API-migrated mail, it can be configured by client to be based on the Date header.

编号:https://developers.google.com/gmail/api/v1/reference/users/messages

根据上述 Google 文档,这是一种方法:

//Some epoch time in ms
var gmail_date = 1512007768005;

//Get DateTime of epoch ms
var to_date = DateTimeOffset.FromUnixTimeMilliseconds(gmail_date).DateTime;

//This is your timezone offset GMT +8
var offset = 8;

Console.WriteLine(to_date - new TimeSpan(offset *-1, 0, 0));

得到你11/30/2017 10:09:28 AM

关于c# - 将 Gmail 日期转换为 Gmail 帐户中显示的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47564487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com