关于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);
        }
    }

时间: 2024-08-04 18:36:47

关于DateTime的自定义转换的相关文章

背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换

[源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 绑定 DataContextChanged - FrameworkElement 的 DataContext 发生变化时触发的事件 UpdateSourceTrigger - 数据更新的触发方式 对绑定的数据做自定义转换 示例1.演示 DataContextChanged 的用法Bind/DataContextChanged.xaml <Page x:Class="Windows10.Bind.DataContex

从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值

最近在ASP.NET MVC中遇到一个问题,如题,在使用EF数据模型的时候,要去添加一条新的数据到Sqlserver数据库,在之前项目中并没有出现该异常,所以去扒了扒demo,发现有几个字段(数据库类型为datetime)savechange的时候默认绑定了datetime.now.问题就在这里,我的新项目并没有给定这几个字段的数据.下面总结下: 触发该错误的条件如下: SQL Server数据库版本中的字段类型为datetime2 数据库中,某个要进行Add或者Edit的字段的数据类型为dat

logback自定义转换的问题

上个星期领导交给我一个任务,就是写logback自定义转换实现以下要求 [IP][文件名][行号][YYMMDD][HHMMSS.sss][线程名称][socket号][代码包和函数名.行号]-[卡号|单位|商户|终端|交易类型][message] eg: 输出结果[192.168.1.134][comm.20880.log][100][16-11-07][19:20:01.002][fixThread001][00000000000020002222222222222001][com.brid

C# DateTime与时间戳转换

原文:C# DateTime与时间戳转换 C# DateTime与时间戳的相互转换,包括JavaScript时间戳和Unix的时间戳. 1. 什么是时间戳 首先要清楚JavaScript与Unix的时间戳的区别: JavaScript时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数. Unix时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至

解决从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值的问题

场景: ASP.NET MVC项目中,使用entity framework添加数据遇到"从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值"这个错误问题.后经过问题的排查,是由于C#代码中实体类所传入的DateTime类型属性没有赋值的原因,其中主要涉及到SQL Server 中datetime2的时间精度和日期范围的问题. 条件分析: SQL Server数据库版本中有datetime2类型 数据库中,某个要进行添加或者修改操作的字段的数据类型为

网站后端_Python+Flask.0006.FLASK地址转换之默认转换与自定义转换?

动态规则: 说明: URL规则可添加变量部分,也就是说将符合同种规则的URL抽象成一个URL模式 @app.route('/instance/<uuid>/') def instance(uuid):     return 'Instance: {}'.format(uuid) 注意: 尖括号中内容是动态的,凡是匹配到/instance/前缀的都会被映射到这个路由上,在内部把uuid作为参数而获得,默认类型为字符串 默认转换: 说明: 可通过<converter:variable_nam

python timestamp和datetime之间的转换

做开发中难免时间类型之间的转换, 最近就发现前端js和后端django经常要用到这个转换, 其中jsDate.now()精确到毫秒,而Python中Datetime.datetime.now()是精确到微秒的. 1. 字符串日期时间转换成时间戳 # '2015-08-28 16:43:37.283' --> 1440751417.283 # 或者 '2015-08-28 16:43:37' --> 1440751417.0 def string2timestamp(strValue): try

DateTime和字符串转换问题

DateTime和string之间的相互转换经常碰到,可就这么简单的一个转换其中也有些需要注意的地方. 1 static void Main(string[] args) 2 { 3 string format = @"yyyy/MM/dd HH:mm:ss"; 4 DateTimeFormatInfo dtfi = DateTimeFormatInfo.InvariantInfo; 5 6 DateTime dateTime = new DateTime(2014, 11, 2, 1

mysql时间属性之时间戳和datetime之间的转换

一.datetime转换为时间戳     方案一:强制转换字段类型 1 use`nec`; 2 SET SQL_SAFE_UPDATES=0; 3 ALTER TABLE `usr_user_info` CHANGE COLUMN `registerTime` `registerTime` BIGINT(20) NOT NULL COMMENT '注册时间' , 4 ALTER TABLE `usr_user_info` CHANGE COLUMN `lastLoginTime` `lastLo