当前位置:网站首页>从Excel读取各种格式的日期转换为目标格式
从Excel读取各种格式的日期转换为目标格式
2022-06-09 07:04:00 【高彬】
程序读取excel中日期时,可能遇到各种各样的格式,转换时经常遇到错误,本示例使用C#程序演示如何转换为目标格式:
public static string ToDate(object val)
{
if (val==null || string.IsNullOrEmpty(val.ToString()))
{
return "0001-01-01";
}
string dateStr = val.ToString();
if (new Regex(@"^\d{4}-\d{2}-\d{2}$").IsMatch(dateStr))
{
//常规日期格式
}
else if(new Regex(@"^[0-9]{1,7}$").IsMatch(dateStr))
{
return DateTime.FromOADate(double.Parse(dateStr)).ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
else if (new Regex(@"^\d{8,}$").IsMatch(dateStr))
{
return DateTime.ParseExact(dateStr, "yyyyMMdd", null).ToString("yyyy-MM-dd");
}
else if (new Regex(@"^\d{1,2}-\d{1,2}月-\d{2,4}$").IsMatch(dateStr))
{
dateStr = new Regex("[\u4e00-\u9fff]").Replace(dateStr, "");
string[] temp = dateStr.Split('-');
dateStr = $"{temp[2]}-{temp[1]}-{temp[0]}";
}
else
{
dateStr = new Regex("[\u4e00-\u9fff]").Replace(dateStr, "");
dateStr = dateStr.Replace('/', '-').Replace('\\', '-').Replace('.', '-');
}
string[] dataArr = dateStr.Split('-');
if (dataArr.Length == 0)
{
return "0001-01-01";
}
string year = dataArr[0];
string month = dataArr.Length >= 1 ? dataArr[1] : "01";
string day = dataArr.Length >= 2 ? dataArr[2] : "01";
string date= $"{year}-{month}-{day}";
string format = $"{"".PadLeft(year.Length, 'y')}-{"".PadLeft(month.Length, 'M')}-{"".PadLeft(day.Length, 'd')}";
return DateTime.ParseExact(date, format, null).ToString("yyyy-MM-dd");
}边栏推荐
- UML series articles (26) architecture modeling -- artifacts
- MongoDB框架零基础入门(未完)
- Can I use machine learning programming without knowing mathematics?
- No editing software is needed to teach you how to edit videos simply and quickly
- 关于用户消息的推送
- Champignon Street publie les résultats de l'exercice 2022: les pertes du deuxième semestre ont diminué de 50% d'une année sur l'autre
- [SDU project training level 2019] personal home page display + personal summary
- [STL] Introduction to the use of set and map
- 【SDU项目实训2019级】个人主页页面展示+个人总结
- UML series articles (23) advanced behavior -- process and thread
猜你喜欢

UML series articles (26) architecture modeling -- artifacts

Database operation statement

defineProperty

Batch writing tidb to improve writing efficiency

DeFi 去風險:分析去中心化系統中的系統性風險
![[C language] summary of document knowledge points](/img/d3/dd5fae70bf7b15280ec32d50aca91b.jpg)
[C language] summary of document knowledge points

UML series articles (23) advanced behavior -- process and thread

Performance test process, index, implementation method and summary

287. 寻找重复数

线程的调度、线程的优先级
随机推荐
Use postman to simulate a request to carry a token
对服装纺织行业的认知
Jump from one pit to another
defineProperty
当内卷风波及代码领域,看Alluxio将会采取怎样的块分配策略
It is so simple to realize the rotation chart!!!
Mongodb basic understanding
Fourier transform signal processing
新职业:数字化管理师太抢手,只要来面试,企业就会发offer
MySQL of quantitative transactions - MySQL database events
[SDU project training level 2019] personal home page display + personal summary
Hummingbird e203 image recognition -- to be continued
技术分享| 浅谈调度平台设计
Camtasia 2022 release update function introduction
[work direction] CONDA common command summary
UML series articles (23) advanced behavior -- process and thread
【系统分析师之路】第十八章 复盘系统安全分析与设计(加密与解密)
Raspberry pie installation opencv - pro test available
Quit smoking log_ 02 (day_06)
Fastadmin custom exported excl table name + time