DateTime 转化成Byte数组 ,相反byte数组转化成Datetime?

时间:2022-07-12 11:47:35
怎么将DateTime 转化成Byte数组 ,相反byte数组转化成Datetime? C#实现。

5 个解决方案

#1


DateTime now = DateTime.Now;

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

#2


还有这么用的?学习~

#3


引用 1 楼 csu_dog 的回复:
DateTime now = DateTime.Now;

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

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");
        }
    }
}


测试通过

#4


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

#5


时间处理的函数很多 条条大路

#1


DateTime now = DateTime.Now;

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

#2


还有这么用的?学习~

#3


引用 1 楼 csu_dog 的回复:
DateTime now = DateTime.Now;

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

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");
        }
    }
}


测试通过

#4


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

#5


时间处理的函数很多 条条大路