日期格式化C#

时间:2013-07-09 19:11:13

标签: c# datetime

我在将此日期格式转换为其他格式时遇到问题。我希望有人在这里帮助我。

这是我的代码:

string fromFormat = "ddd, dd MM yyyy HH:mm:ss zzz"; 
string toFormat = "yyyy-MM-dd";

DateTime newDate = DateTime.ParseExact("Mon, 25 03 2013 00:00:00 GMT", fromFormat, null);

Console.WriteLine(newDate.ToString(toFormat));

------- -------- EDIT

我可以将日期从22更改为25,从而摆脱了我的错误。我的新问题是试图将时区从GMT转换为EST。有人会有任何想法吗?

-------编辑#2 -------

这是我目前的代码。我仍然遇到时区转换问题。

var date = "Mon, 25 03 2013 00:00:00 GMT";

// Cuts off "GMT" portion of string
string newdate = date.Substring(0, 24);

// Switches the output of date
string fromFormat = "ddd, dd MM yyyy HH:mm:ss";
string toFormat = "yyyy-MM-dd";

DateTime newDate = DateTime.ParseExact(newdate, fromFormat, null);
string finaldate = newDate.ToString(toFormat);

// Output final date
Console.WriteLine(finaldate);

-------编辑#3 -------

代码:

var input = "Mon, 25 03 2013 00:00:00 GMT";
var inner = input.Substring(0, 24);
var format = "ddd, dd MM yyyy HH:mm:ss";
var zoneId = "Eastern Standard Time";

var parsed = DateTime.ParseExact(inner, format, CultureInfo.InvariantCulture);

var utcDateTime = DateTime.SpecifyKind(parsed, DateTimeKind.Utc);
var eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTime, zoneId);


Console.WriteLine(eastern);

错误:

Unhandled Exception: System.TimeZoneNotFoundException: Exception of type
   'System.TimeZoneNotFoundException' was thrown.
at System.TimeZoneInfo.FindSystemTimeZoneByFileName (System.String id, System.String
   filepath) [0x00000] in :0 
at System.TimeZoneInfo.FindSystemTimeZoneById (System.String id) [0x00000] in :0 
at System.TimeZoneInfo.ConvertTimeBySystemTimeZoneId (DateTime dateTime, System.String 
   destinationTimeZoneId) [0x00000] in :0 
at Program.Main () [0x00000] in :0 

任何帮助将不胜感激!谢谢!

-------最终编辑-------

这最终改变了时区并转换为我需要的格式。特别感谢@MattJohnson的所有帮助!

// Cuts off 'GMT' portion of string
var newdate = date.Substring(0, 24);

var fromFormat = "ddd, dd MM yyyy HH:mm:ss";
var toFormat = "yyyy-MM-dd";
var zoneId = "Eastern Standard Time";


var parsed = DateTime.ParseExact(newdate, fromFormat, CultureInfo.InvariantCulture);

// Specifies UTC time and converts it to EST timezone
var utcDateTime = DateTime.SpecifyKind(parsed, DateTimeKind.Utc);
var eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTime, zoneId);

// Converts date to final format needed
var finaldate = eastern.ToString(toFormat);

7 个答案:

答案 0 :(得分:5)

string fromFormat = "ddd, dd MM yyyy HH:mm:ss zzz"; 

错误是您的zzz,它是expecting the numerical representation of the timezone,而不是时区的缩写。

所以可接受的版本是

DateTime newDate = DateTime.ParseExact("Mon, 22 03 2013 00:00:00 +0:00", fromFormat, null);

但是这会抛出不同的FormatExecption,消息“字符串未被识别为有效的DateTime,因为星期几不正确。”如果你使the Monday to Friday correction解析有效< / p>

DateTime newDate = DateTime.ParseExact("Fri, 22 03 2013 00:00:00 +0:00", fromFormat, null);

我认为没有格式说明符可以采用时区的文本缩写版本。

答案 1 :(得分:2)

正如其他人所指出的那样,你拥有的输入值是自我不一致的。它指的是2013年3月22日星期一,实际上是星期五。因此,您应该回到源数据并找出发生这种情况的原因。我相信你已经听过这样的说法,&#34; Garbage In,Garbage Out&#34;。

如果您确定要忽略星期几,并且您确定时区将始终为GMT,那么您可以这样做:

var input = "Mon, 22 03 2013 00:00:00 GMT";
var inner = input.Substring(5, 19);
var format = "dd MM yyyy HH:mm:ss";
var parsed = DateTime.ParseExact(inner, format, CultureInfo.InvariantCulture);
var utcDateTime = DateTime.SpecifyKind(parsed, DateTimeKind.Utc);

请注意,我明确将类型设置为UTC,因为您引入了GMT值。 GMT和UTC在所有实际用途中都是相同的。

如果可能会传递其他时区值,那么请提供不同可能输入的样本,也许我们可以找到适应它的方法。

除此之外 - 此字符串看起来与RFC822/RFC1123 formatted dates非常相似,除非您将月份作为数字而不是三个字母的缩写之一传递。你是故意这样做的吗?如果是这样,您已经破坏了此格式的规范。如果您打算从数据中删除可能可本地化的字符串,请使用专为此设计的格式,例如ISO8601 / RFC3339

开启时区

你说你想要转换为EST,你可能意味着&#34;美国东部时间&#34 ;,它在EST和EDT之间交替进行夏令时。尽管如此,Windows时区数据库使用的是&#34;东部标准时间&#34;引用这两个值 - 所以尽量不要在这一点上感到困惑。

如上所示,如果您的日期为DateTime种类Utc,则可以使用以下内容将其转换为“东部时间”:

var zoneId = "Eastern Standard Time"; // don't get confused here! :)
var eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTime, zoneId);

小心你对这个值的处理方式。如果您要将其显示给用户,那么您没问题。就这样做:

var s = eastern.ToString("g"); // or whatever format you want.

但是如果您使用此算法,或将其存储为记录的事件时间,则可能会在结果中引入错误。这是由于夏令时转换。避免这种情况的一种方法是改为使用DateTimeOffset类型:

var utcDateTimeOffset = new DateTimeOffset(utcDateTime, TimeSpan.Zero);
var eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTimeOffset, zoneId);

现在,当您使用这些值时,偏移量会捕获任何歧义。

如果这对您来说非常困惑,请不要担心 - 您并不孤单。您可能想尝试使用Noda Time。它会阻止你在脚下射击。

答案 2 :(得分:1)

要查看错误,请使用fromString打印DateTime

DateTime dt = new DateTime(2013, 3, 22);
string s = dt.ToString(fromFormat);

你会看到输出是:

Fri, 22 03 2013 00:00:00 -04:00

这就是它所期望的格式。

您可以从 this article 的缩写中获得一些帮助。

答案 3 :(得分:0)

22 03 2013 00:00:00 GMT不是星期一,而是星期五

            string fromFormat = "ddd, dd MM yyyy HH:mm:ss zzz";
            string toFormat = "yyyy-MM-dd";

            Console.WriteLine(DateTime.ParseExact("Fri, 22 03 2013 00:00:00 +00:00", fromFormat,
                             CultureInfo.InvariantCulture).ToString(toFormat));

答案 4 :(得分:0)

有两个问题:

1)所选日期为星期五而非星期一

2)'zzz'需要正负0:00

所以,要让它发挥作用将是:

string fromFormat = "ddd, dd MM yyyy HH:mm:ss zzz";
string toFormat = "yyyy-MM-dd";

DateTime newDate = DateTime.ParseExact("Fri, 22 03 2013 00:00:00 +0:00", fromFormat, null);

Console.WriteLine(newDate.ToString(toFormat));

答案 5 :(得分:0)

没有简单的内置方法可以将时区缩写转换为偏移或专有名称。本主题在此讨论:

Timezone Abbreviations

您需要创建一个要使用的缩写表,映射到偏移量或专有名称,或者您需要更改传入日期/时间字符串的格式以使用偏移量。

答案 6 :(得分:0)

string fromFormat = "ddd, dd MM yyyy HH:mm:ss zzz"; 
string toFormat = "yyyy-MM-dd";

DateTime newDate = DateTime.ParseExact("Mon, 22 03 2013 00:00:00 +00:00", fromFormat, null);

Console.WriteLine(newDate.ToString(toFormat));
相关问题