从txt文件读取的DateTime [英] DateTime read from txt file

查看:78
本文介绍了从txt文件读取的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取类似("05.10/2012 5:30:29 PM")的字符串并在DateTime值中使用它?

How to read a string like ("05.10/2012 5:30:29 PM") and use it in a DateTime value ?

推荐答案

Hello

试试这个:
Hello

Try this:
DateTime myDateTime = DateTime.Parse("05.10/2012 5:30:29 PM");




or

DateTime myDateTime;
DateTime.TryParse("05.10/2012 5:30:29 PM", out myDateTime)



详细信息: http://msdn.microsoft.com/en-us/library/1k1skd40.aspx [ ^ ]



More information: http://msdn.microsoft.com/en-us/library/1k1skd40.aspx[^]


假设每个日期都在单独的行中,则可以使用
读取所有日期
Assuming each date is on a separate line, you can read them all with
string[] lines = File.ReadAllLines(path);


然后,您可以使用Datetime.ParseExact或DateTime.TryParseExact将每个转换为DateTime:


You can then convert each one to a DateTime using Datetime.ParseExact, or DateTime.TryParseExact:

foreach (string line in lines)
   {
   DateTime dt = DateTime.ParseExact(line, "dd.MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
   ...
   }


这里有一个DateTime格式代码列表:为显示-格式字符串说明 [ ^ ]


There is a list of DateTime format codes here: Formatting a DateTime for display - format string description[^]


这篇关于从txt文件读取的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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