C# 带字母的格式的时间字符串转为DateTime的方法

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上海好程序员

给上海好程序员加个鸡腿!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值