【问题标题】:Format A Date With Offset格式化带偏移量的日期
【发布时间】:2017-01-20 23:43:20
【问题描述】:

我正在尝试使用 Offset 更改字符串 DateTime 值。这是我尝试过的过程,但最后, datetime 和 datetime1 都打印了它们的初始值。我想要的输出是将 datetime1 格式化为正确的 Offset 以便它反映 datetime

2016 年 1 月 10 日下午 5:18
01/10/2016 5:18 PM-05:00

string datetime = "2017-01-10T17:18:00-05:00";
string datetime1 = "1/10/2016 3:18:00 PM";

DateTimeOffset dateTimeOffset = DateTimeOffset.Parse(datetime);
TimeSpan tspan = dateTimeOffset.Offset;
DateTimeOffset alteredDate = new     DateTimeOffset(Convert.ToDateTime(datetime1)).ToOffset(tspan);

UAB = Convert.ToString(DateTimeOffset.Parse(alteredDate.ToString()));

Console.WriteLine(datetime);
Console.WriteLine(UAB);
Console.ReadLine();

编辑
在单步执行我的代码时,我注意到tpsan 的值是-05:00- 符号可能是导致代码无法正确转换的原因吗?

【问题讨论】:

  • 你知道,日期是完全可用的结构。无需将它们存储为字符串,DateTime.ToString();将随时以您想要的任何格式显示。
  • @Plutonix - 我同意你的看法,日期在我收到的文件中作为刺痛收到

标签: c# datetime timezone utc timezone-offset


【解决方案1】:

使用不同的构造函数:

DateTimeOffset alteredDate = 
    new DateTimeOffset( Convert.ToDateTime( datetime1 ), tspan );

这是文档:

//
// Summary:
//     Initializes a new instance of the System.DateTimeOffset structure using the specified
//     System.DateTime value and offset.
//
// Parameters:
//   dateTime:
//     A date and time.
//
//   offset:
//     The time's offset from Coordinated Universal Time (UTC).
//
// Exceptions:
//   T:System.ArgumentException:
//     dateTime.Kind equals System.DateTimeKind.Utc and offset does not equal zero.-or-dateTime.Kind
//     equals System.DateTimeKind.Local and offset does not equal the offset of the
//     system's local time zone.-or-offset is not specified in whole minutes.
//
//   T:System.ArgumentOutOfRangeException:
//     offset is less than -14 hours or greater than 14 hours.-or-System.DateTimeOffset.UtcDateTime
//     is less than System.DateTimeOffset.MinValue or greater than System.DateTimeOffset.MaxValue.
public DateTimeOffset(DateTime dateTime, TimeSpan offset);

【讨论】:

  • 这不是将偏移量添加到 datetime1 它仍然显示不正确的值
  • @RedLightGreenLight:这确实是所问问题的正确答案。如果您传入DateTimeTimeSpan,它将完全按照指定创建DateTimeOffset。但是如果你只传入一个DateTime,它会根据你传入的DateTimeKind 属性对你想要的偏移量做出一些假设。然后ToOffset 将切换偏移量以及本地时间匹配 - 你不想要的。所以,如果你遵循这个答案,你真的应该得到你所要求的。
【解决方案2】:

您收到的 DateTimeOffset 对象已针对时区进行了调整。

string output = "";

// Parsing with explicit timezones
var withZeroOffset = DateTimeOffset.Parse("2017-01-10T17:18:00-00:00"); // parsed as UTC
var withOffset = DateTimeOffset.Parse("2017-01-10T17:18:00-05:00"); // Parsed as specific timezone
var withoutOffset = DateTimeOffset.Parse("2017-01-10T17:18:00"); // Parsed as my timezone
output += "All Times:\n" + withZeroOffset + "\n" + withOffset + "\n" + withoutOffset + "\n\n";

// Modifying timezones
var inputUtc = DateTimeOffset.Parse("2017-01-10T17:18:00Z").ToOffset(TimeSpan.Zero);
output += "Time UTC: " + inputUtc + "\n";
var minusFive = inputUtc.ToOffset(TimeSpan.FromHours(-5));
output += "Time @ -5:00: " + minusFive + "\n";
var myOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
var myTime = inputUtc.ToOffset(myOffset);
output += "Time in my timezone: (" + myOffset.TotalHours + ":00): " + myTime + "\n";

Console.WriteLine(output);

在我的机器上,使用我的时区,我得到以下输出:

All Times:
1/10/2017 5:18:00 PM +00:00
1/10/2017 5:18:00 PM -05:00
1/10/2017 5:18:00 PM -08:00

Time UTC: 1/11/2017 1:18:00 AM +00:00
Time @ -5:00: 1/10/2017 8:18:00 PM -05:00
Time in my timezone: (-8:00): 1/10/2017 5:18:00 PM -08:00

我假设您的显式偏移量与您的实际时区匹配,这就是您两次看到相同答案的原因。要显式更改 DateTimeOffset 对象的时区,请使用 .ToOffset()。

【讨论】:

  • 我认为 OP 想给它一个不同的偏移量。
  • 当您收集myOffset 时,您假设当前 偏移量始终是您所在时区的正确偏移量。这是一个危险的假设,因为它可能已经改变了夏令时。真的,你永远不应该使用TimeZone 类,而是使用TimeZoneInfo。你可以使用TimeZoneInfo.ConvertTime,这样你就不用再打电话给ToOffset了。
  • 哦,最后,您假设所有偏移量都是整小时。这在像印度这样UTC+05:30 的地方会失败。有许多 :30 和一些 :45 偏移量分布在世界各地。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多