栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

        /// 
        /// 获取时间[Wed Sep 29 10:02:41 2021 +0800]
        /// 
        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}]rn{1}", time, ex.Message);
                return DateTime.MinValue;
            }
        }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283889.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号