如何将字符串日期数组转换为实际日期数组 [英] How to convert an array of string dates to an array of actual dates

查看:98
本文介绍了如何将字符串日期数组转换为实际日期数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我有以下日期列表:



01/01/2008

03/01/2008

04/01/2008

07/01/2008

08/01/2008
09/01/2008



我正在通过在新文件上使用split()函数将其从文本文件读取为类型字符串数组line。我需要将这个字符串日期数组转换为实际日期数组,但此时我似乎陷入困境。



请参阅下文:



Hi
I have the following list of dates :

01/01/2008
03/01/2008
04/01/2008
07/01/2008
08/01/2008
09/01/2008

I'm reading this from a text file into a array of type string by using split() function on a new line.I than need to convert this array of string dates into an array of actual dates but I seem to be "stuck" at this point.

Please see below :

StreamReader sr = new StreamReader("C:\\Users\\temp\\readtest.txt");


line = sr.ReadToEnd();

string[] commaSeparator = new string[] { "\n" };

string[] results = line.Split(commaSeparator, StringSplitOptions.None);
                
                
foreach (string s in results)
       {
        DateTime results2 = Convert.ToDateTime(s);
         //Console.WriteLine(s);
        } 





有人可以帮助。我不需要这样的答案,只是放在正确的道路上。



谢谢



Could someone please assist.I do not need the answer as such but just to be placed on the right path.

Thanks

推荐答案

阅读所有关于 DateTime.TryParse [ ^ ]方法。
Read all about DateTime.TryParse[^] method.


这篇关于如何将字符串日期数组转换为实际日期数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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