关于DateTime的自定义转换
关于DateTime的自定义转换。把字符串时间转换成可以供DateTime类型识别的字符串类型的粗略实现。
/// <summary>
/// 把从数据库中读取的字符串的CurrentTime数据转换成可以被DateTime类型识别的格式
/// 可识别的格式如: 2009-06-15T13:45:30.6175425 -> 6175425
/// </summary>
public class CustomDateTimeConverter
{
public CustomDateTimeConverter() { }
//实现在时分秒间插入“:”
public string TimeStringConverter(string s)
{
string s2 = InsertChar(s, 3, ':');
string s3 = InsertChar(s2, 1, ':');
return s3;
}
//在第m+1个位置后插入一个字符c
// if s.length < m, insert into the end ?????
public string InsertChar(string s, int m, char c)
{
if (s.Length <= m) return s + c;
else return s.Insert(m + 1, new string(c, 1)); //int length = s.Length;
//length = length + 1;
//char[] temp = new char[length];
//for (int i = length - 1; i > m; i--)
//{
// temp[i] = s.ToArray()[i - 1];
//}
//temp[m + 1] = c;
//for (int i = 0; i <= m; i++)
//{
// temp[i] = s.ToArray()[i];
//}
//s = new string(temp);
//return s;
} public string DateStringConverter(string s)
{
string s1= InsertChar(s, 5, '-');
string s2 = InsertChar(s1, 3, '-');
return s2;
} /* Get sub string. */
private static string subString(string s, int index, int len)
{
if (s.Length <= index) return string.Empty;
else if (s.Length <= (index + len)) return s.Substring(index);
else return s.Substring(index, len);
} /* Parse a string into the int value with min and max. */
private static int toInt(string s, int min, int max)
{
int tmp;
int.TryParse(s, out tmp); if (tmp < min) tmp = min;
else if (tmp > max) tmp = max; return tmp;
} /* Try to parse date(YYYYMMDD) and time(HHMMSS.MMM) into DateTime.
*
* return default value if input a invalid parameter.
* */
public static DateTime TryToDateTime(string date, string time)
{
DateTime min = DateTime.MinValue;
DateTime max = DateTime.MaxValue; // year
int index = 0;
int year = toInt(subString(date, index, 4), min.Year, max.Year);
// month
index += 4;
int month = toInt(subString(date, index, 2), min.Month, max.Month);
// day
index += 2;
int day = toInt(subString(date, index, 2), min.Day, max.Day);
// hour
index = 0;
int hour = toInt(subString(time, index, 2), min.Hour, max.Hour);
// minute
index += 2;
int minute = toInt(subString(time, index, 2), min.Minute, max.Minute);
// second
index += 2;
int second = toInt(subString(time, index, 2), min.Second, max.Second);
// millisecond
index += 3;
int millisecond = toInt(subString(time, index, 3), min.Millisecond, max.Millisecond); return new DateTime(year, month, day, hour, minute, second, millisecond);
}
}
关于DateTime的自定义转换的更多相关文章
- 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换
[源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...
- 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换
介绍背水一战 Windows 10 之 绑定 DataContextChanged - FrameworkElement 的 DataContext 发生变化时触发的事件 UpdateSourceTr ...
- C# MVC 用户登录状态判断 【C#】list 去重(转载) js 日期格式转换(转载) C#日期转换(转载) Nullable<System.DateTime>日期格式转换 (转载) Asp.Net MVC中Action跳转(转载)
C# MVC 用户登录状态判断 来源:https://www.cnblogs.com/cherryzhou/p/4978342.html 在Filters文件夹下添加一个类Authenticati ...
- System.Data.SqlClient.SqlException: 从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值
System.Data.SqlClient.SqlException: 从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值.解决办法是: 而这位大哥提出的解决办法 ...
- sql server报【从varchar数据类型到datetime数据类型的转换产生一个超出范围的值】错误的解决办法
产生这个错误的原因是在使用convert函数将给定的日期字符串转换为日期类型的时候,因为datetime这个数据类型有时间数值的范围限定,当超出时间范围时就抛出这个错误. 如果类型是[datetime ...
- 5、flink常见函数使用及自定义转换函数
代码地址:https://gitee.com/nltxwz_xxd/abc_bigdata 一.flink编程方法 获取执行环境(execution environment) 加载/创建初始数据集 对 ...
- Spring自定义转换类,让@Value更方便
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 关于配置的文章已经写了很多,相信看过的人还是会有一定收获的,系列文章可阅读:南瓜慢说-配置相关文章.对于@Val ...
- 把excel的数据导入到SQLSERVER里面,excel的字符串时间在导入sql库显示datetime 数据类型的转换产生一个超出范围的值
这是我Excel导入的数据,准备把这个varchar(50)时间导入我的userInfo表中的出生日期字段datatime,如果你的数据正常,是可以导入的, 但是有些日期可能超出datatime的最大 ...
- Nullable<System.DateTime>日期格式转换 (转载)
一.问题 1.html页面中时间显示出错,数据库中时间是正确的. 原因:没有把DateTime转成String类型. 2. 在C#中,发现不能直接使用ToString("yyyy-MM-d ...
- python time和datetime的常用转换处理
一.time 1.获取当前时间和时区 >>> now = time.time() # 当前时间 float类型 >>> time.strftime("%Y ...
随机推荐
- 使用阿里的ARTHAS跟踪方法耗时
使用命令跟踪一个方法的耗时 在arthas 命令行下输入命令 trace 类全路径 监控的方法 trace com.redxun.bpm.core.service.BpmInstServiceImpl ...
- Pwn2own 2023 Tesla 利用链摘要
Pwn2own 2023 Tesla 利用链摘要 https://www.youtube.com/watch?v=6KddjKKKEL4 攻击链: 利用蓝牙协议栈自己实现的 BIP 子协议中的堆溢出, ...
- Fake JSON Server
Fake JSON Server https://github.com/ttu/dotnet-fake-json-server Fake JSON Server 是 Fake REST API,可以作 ...
- 设置Docker的默认文件存储位置
对于windows下,直接修改docker desktop界面的配置项目.对于rocky linux下面,对应的配置文件存储在: vim /etc/docker/daemon.json 文件可以配置镜 ...
- 【Java】【SpringBoot】CP03:热部署
This article is written by Xrilang(Chinese Name:萌狼蓝天) If you want find me ,You can contact me in Bil ...
- alibabacloud-jindodata
https://github.com/aliyun/alibabacloud-jindodata https://github.com/aliyun/alibabacloud-jindodata/bl ...
- Spring Security并结合JWT实现用户认证(Authentication) 和用户授权(Authorization)
引言在Web应用开发中,安全一直是非常重要的一个方面.Spring Security基于Spring 框架,提供了一套Web应用安全性的完整解决方案. JwT (JSON Web Token) 是当前 ...
- Qt设置运行时动态库路径的几点说明
随着需求的不断增加,程序不断变大,用到的动态库也越来越多,到了发布程序的时候你会发现和可执行文件同一目录下文件数量真多(比如著名的金融软件 https://www.webull.com/ 哎呀我去,目 ...
- Qt编写安防视频监控系统40-onvif线程处理
一.前言 整个onvif模块大部分的功能都有了以后,除了在demo上点点按钮可以执行获取结果显示外,最终还是要应用到视频监控中,在按钮上点点和系统中后台自动运行是两码事,比如onvif校时和事件订阅, ...
- CentOS 集群初始化设置
0. 前置操作 centos-7.9.2009-isos-x86_64安装包下载_开源镜像站-阿里云 下载CentOS-7-x86_64-DVD-2009.iso即可 1. 配置静态网络 1.1 查看 ...