我在爬取疫情消息录入自己的数据库时,爬取到的日期都是MM.dd格式的
DateTime[] date = StringArrayToDateTime("1.26,1.27,1.28,1.29,1.30,1.31,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,2.10,2.11,2.12,2.13,2.14,2.15,2.16,2.17,2.18,2.19,2.20,2.21,2.22,2.23,2.24,2.25,2.26,2.27,2.28,2.29,3.1,3.2,3.3,3.4,3.5,3.6,3.7,3.8,3.9,3.10,3.11,3.12,3.13,3.14,3.15,3.16,3.17,3.18,3.19,3.20,3.21,3.22,3.23,3.24,3.25,3.26,3.27,3.28,3.29,3.30,3.31,4.1,4.2,4.3,4.4,4.5,4.6,4.7,4.8,4.9,4.10,4.11,4.12,4.13,4.14,4.15,4.16,4.17,4.18,4.19,4.20,4.21,4.22,4.23,4.24,4.25,4.26,4.27,4.28,4.29,4.30,5.1,5.2,5.3,5.4,5.5,5.6,5.7,5.8,5.9,5.10,5.11,5.12,5.13,5.14,5.15,5.16,5.17,5.18,5.19,5.20,5.21,5.22,5.23,5.24,5.25,5.26,5.27,5.28,5.29,5.30,5.31,6.1,6.2,6.3,6.4,6.5,6.6,6.7,6.8,6.9,6.10,6.11,6.12,6.13,6.14,6.15,6.16,6.17,6.18,6.19,6.20,6.21,6.22,6.23,6.24,6.25,6.26".Split(','));
[C#] 纯文本查看复制代码
/// <summary>
/// string[]转DateTime[]
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static DateTime[] StringArrayToDateTime(string[] str)
{
List<DateTime> a = new List<DateTime>();
try
{
foreach (string i in str)
{
string s = string.Format("2020.{0}", i);
a.Add(DateTime.ParseExact(s,"yyyy.MM.dd",System.Globalization.CultureInfo.InvariantCulture));
}
return a.ToArray();
}
catch (Exception)
{
return null;
//throw;
//return a.ToArray();
}
}