将 Gmail 日期转换为 Gmail 帐户中显示的日期时间
Convert Gmail Date to your DateTime as displayed in Gmail Account
我在我的 c# ASP.net 应用程序中使用 Gmail API 接收收件箱消息。所有邮件都来自不同的时区 (states/countries),我想在我自己的时区显示日期,因为它在 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 收件箱邮件中获得的一些值:
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.
REF: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));
我在我的 c# ASP.net 应用程序中使用 Gmail API 接收收件箱消息。所有邮件都来自不同的时区 (states/countries),我想在我自己的时区显示日期,因为它在 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 收件箱邮件中获得的一些值:
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.
REF: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));