/// <summary>
/// 获取时间[Wed Sep 29 10:02:41 2021 +0800]
/// </summary>
public static DateTime ParseExact(string time)
{
try
{
var cells = time.Split(' ');
int year = cells[4].ToInt();
int month = 0;
switch (cells[1])
{
case "一月":
case "January":
case "Jan":
month = 1;
break;
case "二月":
case "February":
case "Feb":
month = 2;
break;
case "三月":
case "March":
case "Mar":
month = 3;
break;
case "四月":
case "April":
case "Apr":
month = 4;
break;
case "五月":
case "May":
month = 5;
break;
case "六月":
case "June":
case "Jun":
month = 6;
break;
case "七月":
case "July":
case "Jul":
month = 7;
break;
case "八月":
case "August":
case "Aug":
month = 8;
break;
case "九月":
case "September":
case "Sep":
case "Sept":
month = 9;
break;
case "十月":
case "October":
case "Oct":
month = 10;
break;
case "十一月":
case "November":
case "Nov":
month = 11;
break;
case "十二月":
case "December":
case "Dec":
month = 12;
break;
}
int day = cells[2].ToInt();
var subCells = cells[3].Split(':');
int hour = subCells[0].ToInt();
int minute = subCells[1].ToInt();
int second = subCells[2].ToInt();
return new DateTime(year, month, day, hour, minute, second);
}
catch (Exception ex)
{
LogUtil.Print("Try to parse to time error with: [{0}]\r\n{1}", time, ex.Message);
return DateTime.MinValue;
}
}
C# 带字母的格式的时间字符串转为DateTime的方法
最新推荐文章于 2023-11-18 07:33:42 发布