DATETIME BYTE数组

来源:互联网 发布:sql sever 数据挖掘 编辑:程序博客网 时间:2024/06/11 23:12

 Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bytex(7) As Byte
        bytex(0) = 0
        bytex(1) = 0
        bytex(2) = 0
        bytex(3) = 0
        bytex(4) = 96
        bytex(5) = 110
        bytex(6) = 227
        bytex(7) = 64

        Dim dtx As Date
        'Dim ss As String

        'Dim year As Integer = bytex(7)
        'Dim month As Integer = bytex(6)
        'Dim day As Integer = bytex(5)
        'Dim c1 As System.Globalization.Calendar = New System.Globalization.GregorianCalendar
        'dtx = c1.ToDateTime(year, month, day, 0, 0, 0, 0, 0)

        ''ss = System.BitConverter.ToString(bytex, 0)
        ''ss = Strings.Replace(ss, "-", "")
        ''ss = Strings.StrReverse(ss)
        ''dtx = aaax(bytex())
        dtx = GetDateFromBytes(bytex)


        '        解决!()

        '  byte[]   hexString   =   new   byte[]   {   0xA0,   0xE9,   0x88,   0x7D,   0xC7,   0x56,   0x00,   0x40   };   //1900-1-1   1:1:1
        '  long   tickBase   =   DateTime.Parse( "1899-12-30   0:0:0 ").Ticks;   //delphi基础日期在c#中的位置
        '  double   doub   =   Math.Abs(   BitConverter.ToDouble(hexString,   0));//该字节数组代表的基于delphi基础日期的偏移值
        '  long   tickAddition   =   (long)(doub   *   10000000   *   3600   *   24);//;将该偏移值转换为c#表示形式。(doub*1天的总毫微秒)delphi的小数部分表示时间,整数部分表示日期,所以这样处理
        '  DateTime   objDt   =   new   DateTime(tickBase   +   tickAddition);//转换为日期

        '谢谢各位。ruan_hg(阿阮)ParadiseX(夜.夜.夜.夜)Red_angelX(八戒)wsxqaz(原来可以改昵称)

        '/// <summary>
        '        /// 将DATETIME类型的对象转为可用的BYTE数组
        '        /// </summary>
        '        /// <param name="dt"></param>
        '        /// <returns></returns>
        '        private byte[] DateTimeToBytes(DateTime dt)
        '        {
        '            byte[] bytes = new byte[6];
        '            if (dt != null)
        '            {
        '                bytes[0] = Convert.ToByte(dt.Year.ToString().Substring(2, 2),16);
        '                bytes[1] = Convert.ToByte(dt.Month.ToString(), 16);
        '                bytes[2] = Convert.ToByte(dt.Day.ToString(), 16);
        '                bytes[3] = Convert.ToByte(dt.Hour.ToString(), 16);
        '                bytes[4] = Convert.ToByte(dt.Minute.ToString(), 16);
        '                bytes[5] = Convert.ToByte(((int)dt.DayOfWeek).ToString(), 16);
        '                //bytes[5] = 0xA;
        '                //bytes[5] = Convert.ToByte(dt.Second.ToString(), 10);
        '            }
        '            return bytes;
        '        }

        '/// <summary>
        '        /// 将BYTE数组转换为DATETIME类型
        '        /// </summary>
        '        /// <param name="bytes"></param>
        '        /// <returns></returns>
        '        private DateTime BytesToDateTime(byte[] bytes)
        '        {
        '            if (bytes != null && bytes.Length >= 5)
        '            {
        '                int year = 2000 + Convert.ToInt32(BitConverter.ToString(new byte[1] { bytes[0] }, 0));
        '                int month = Convert.ToInt32(BitConverter.ToString(new byte[1] { bytes[1] }, 0));
        '                int date = Convert.ToInt32(BitConverter.ToString(new byte[1] { bytes[2] }, 0));
        '                int hour = Convert.ToInt32(BitConverter.ToString(new byte[1] { bytes[3] }, 0));
        '                int minute = Convert.ToInt32(BitConverter.ToString(new byte[1] { bytes[4] }, 0));
        '                DateTime dt = new DateTime(year, month, date, hour, minute, 0);
        '                return dt;
        '            }
        '        Else
        '            {
        '                return new DateTime();
        '            }
        '        }

    End Sub

    Public Function GetDateFromBytes(ByRef arr() As Byte) As Date
        Dim tickBase, tickAddition As Long
        Dim doub As Double
        Dim dt As Date
        tickBase = DateTime.Parse("1899-12-30   0:0:0 ").Ticks
        doub = Math.Abs(System.BitConverter.ToDouble(arr, 0))
        tickAddition = CLng(doub * 10000000 * 3600 * 24)
        dt = New Date(tickBase + tickAddition)
        Return dt
    End Function
End Class

原创粉丝点击