怎么将DateTime 转化成Byte数组 ,相反byte数组转化成Datetime? C#实现。

解决方案 »

  1.   

    DateTime now = DateTime.Now;byte[] bts = BitConverter.GetBytes(now.ToBinary());
    DateTime rt = DateTime.FromBinary(BitConverter.ToInt64(bts, 0));
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace CS.sql_manage
    {
        public partial class TestCode : Form
        {
            public TestCode()
            {
                InitializeComponent();
            }        private void TestCode_Load(object sender, EventArgs e)
            {
               // DateTime dt = DateTime.Now;
              //  this.textBox1.Text = string.Format("{0:yyyyMMdd}",dt);
    //this.textBox1.Text = dt.ToString("yyyyMMddHHmmss");
               DateTime now = DateTime.Now;            byte[] bts = BitConverter.GetBytes(now.ToBinary());
                DateTime rt = DateTime.FromBinary(BitConverter.ToInt64(bts, 0));
                this.textBox1.Text = rt.ToString("yyyyMMddHHmmss");
            }
        }
    }测试通过
      

  3.   

    要用在网络上?
    为什么不用BinaryStream