日期格式C# [英] Date Formatting C#

查看:196
本文介绍了日期格式C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,这个转换的日期格式到另一种格式。 。我希望在这里有人将能够帮助我



下面是我的代码:

 字符串fromFormat =DDD,年月日HH:MM:SS ZZZ 
串toFormat =YYYY-MM-DD;

的DateTime newDate = DateTime.ParseExact(周一,25 03 2013 00:00:00 GMT,fromFormat,NULL);

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



------- --------编辑



我能够从 22 改变一天摆脱我的错误,以 25 。我的新问题是试图让时区从GMT转换为EST。有没有人有什么想法?



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



下面是我当前的代码,因为它主张。我仍然有一个时区转换问题。

  VAR日期=周一,25 03 2013 00:00:00 GMT ; 

//削减字符串
串newdate = date.Substring(0,24)关闭GMT部分;

//开关日期
串fromFormat =DDD,年月日HH:MM:SS的输出;
串toFormat =YYYY-MM-DD;

的DateTime newDate = DateTime.ParseExact(newdate,fromFormat,NULL);
串finaldate = newDate.ToString(toFormat);

//输出最后日期
Console.WriteLine(finaldate);



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



中的代码:

  VAR输入=星期一25 03 2013 00:00:00 GMT 
变种内= input.Substring(0,24);
VAR格式=DDD,年月日HH:MM:SS;
VAR =了zoneid东部标准时间;

VAR分析= DateTime.ParseExact(内,格式,CultureInfo.InvariantCulture);

VAR utcDateTime = DateTime.SpecifyKind(解析,DateTimeKind.Utc);
VAR东部= TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTime,了zoneid);


Console.WriteLine(东部);



错误:

 未处理的异常:System.TimeZoneNotFoundException:输入
'System.TimeZoneNotFoundException引发的异常。在System.TimeZoneInfo.FindSystemTimeZoneByFileName
(System.String ID,System.String
文件路径)[0x00000]中:0
在System.TimeZoneInfo.FindSystemTimeZoneById(System.String ID)[0x00000]在:在System.TimeZoneInfo.ConvertTimeBySystemTimeZoneId 0
(DateTime的日期时间,System.String
destinationTimeZoneId)[0x00000]中:0 $在Program.Main b $ b()[0x00000]中:0

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



-------最后编辑-------



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



  //速断弦
变种GMT部分newdate = date.Substring(0,24);

VAR fromFormat =DDD,年月日HH:MM:SS;
VAR toFormat =YYYY-MM-DD;
VAR =了zoneid东部标准时间;


VAR分析= DateTime.ParseExact(newdate,fromFormat,CultureInfo.InvariantCulture);

//指定UTC时间并将其转换为EST时区
变种utcDateTime = DateTime.SpecifyKind(解析,DateTimeKind.Utc);
VAR东部= TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTime,了zoneid);

//转换日期最终格式所需的
VAR finaldate = eastern.ToString(toFormat);


解决方案

正如其他人指出的那样,你必须输入值自我不一致。它指的是2013年3月22日为周一,当它实际上是一个星期五。所以,你应该回到你的源数据,并找出为什么发生这种情况。我敢肯定你已经听说过这样一句话,垃圾进,垃圾出。



如果您确定要忽略一周的某一天,你一定该时区将永远是GMT,那么你可以这样做:

  VAR输入=周一,22 03 2013 00: 00:00 GMT; 
变种内= input.Substring(5,19);
VAR格式=年月日HH:MM:SS;
VAR分析= DateTime.ParseExact(内,格式,CultureInfo.InvariantCulture);
VAR utcDateTime = DateTime.SpecifyKind(解析,DateTimeKind.Utc);

请注意,我明确设置的实物UTC,因为你带来GMT值。 GMT和UTC是所有的实际目的是相同的。



如果有可能,其他时区的值将被传递,那么请提供不同的可能输入的采样,也许我们可以。找到一种方法,以适应



顺便说一句 - 这串看起来非常相似的 RFC822 / RFC1123格式的日期的,除非你是路过的月份为数字,而不是三个字母的缩写之一。你有没有做到这一点故意?如果是这样,你已经打破了这种格式的规范。如果你的目的是为了从数据中删除潜在的本地化字符串,那么请使用已专为格式,如的 ISO8601 / RFC3339



在时区



您说您要转换为EST,你大概的意思是美国东部时间,这EST和之间交替美国东部时间为夏令时。尽管如此,Windows时区数据库使用东部标准时间的ID来引用两个值 - 所以尽量不要让这一点迷惑



一旦你有日期作为的DateTime UTC 样的,正如我上面显示,您可以在转换为东部时间与以下内容:

  VAR =了zoneid东部标准时间; //不要混淆这里! :) 
VAR东部= TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTime,了zoneid);



只是要小心你这个值做什么。如果你是它显示给用户,那么一切都OK。只要做到这一点:

 变种S = eastern.ToString(G); //或任何格式的你想要的。 



但是,如果你做数学这一点,或将其存储为一个记录的事件的时候,你可能会引入错误到你的结果。这是由于夏令时转换。要避免这种情况的一种方法是使用的DateTimeOffset 类型,而不是:

  VAR utcDateTimeOffset =新的DateTimeOffset(utcDateTime,TimeSpan.Zero); 
VAR东部= TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTimeOffset,了zoneid);

现在,当您使用这些值工作,任何含糊之处被抵消。

$抓获b
$ b

如果这似乎很迷惑你,不要担心 - 你并不孤单。您可能需要使用野田佳彦时间,而不是尝试。它会阻止你的脚搬起石头砸自己。


I am having an issue converting this date format into another format. I was hoping that somebody on here would be able to help me out.

Here is my code:

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--------

I was able to get rid of my errors by changing the day from 22 to 25. My new issue is trying to get the timezone to convert from GMT to EST. Would anyone have any ideas?

-------EDIT #2-------

Here is my current code as it stands. I am still having issues with a timezone conversion.

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

-------EDIT #3-------

The code:

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

The error:

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 

Any help would be much appreciated! Thanks!

-------FINAL EDIT-------

This is what ended up changing the timezone and converting to the format that I needed. Special thanks to @MattJohnson for all of his help!

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

解决方案

As others pointed out, the input value you have is self-inconsistent. It refers to a March 22 2013 as a Monday when it is actually a Friday. So you should go back to your source data and figure out why that happens. I'm sure you have heard the saying, "Garbage In, Garbage Out".

If you are sure you want to ignore the day of week, and you are certain that the time zone will always be GMT, then you can do this:

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

Note that I explicitly set the kind to UTC, because you are bringing in GMT values. GMT and UTC are identical for all practical purposes.

If it's possible that other time zone values will be passed, then please provide a sampling of different possible inputs and perhaps we can find a way to accommodate that.

As an aside - this string looks very similar to RFC822/RFC1123 formatted dates, except you are passing the month as a number instead of one of the three-letter abbreviations. Did you do this intentionally? If so, you have broken the spec for this format. If your intention was to remove potentially localizable strings from the data, then please use a format that is already designed for that, such as ISO8601/RFC3339.

On Time Zones

You said you want to convert to EST, You probably mean "US Eastern Time", which alternates between EST and EDT for daylight saving time. Despite this, the Windows Time Zone database uses the id of "Eastern Standard Time" to refer to both values - so try not to get confused on that point.

Once you have the date as a DateTime of Utc kind, as I showed above, you can convert that to Eastern Time with the following:

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

Just be careful what you do with this value. If you are displaying it to an user, then you are ok. Just do this:

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

But if you do math with this, or store it as an recorded event time, you are potentially introducing errors into your results. This is due to daylight saving time transitions. One way to avoid this is to use a DateTimeOffset type instead:

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

Now when you work with these values, any ambiguities are captured by the offset.

If this seems very confusing to you, don't worry - you're not alone. You might want to try using Noda Time instead. It will prevent you from shooting yourself in the foot.

这篇关于日期格式C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆