6

I tried converting it to byte array, but a minimum byte array of 5 is created. But I have only 4 bytes only for this date time to stored as byte in my array of byte.

code is like this:

byte[] b = new byte[] {10,12,12,12};
DATETIME t=datetime.now();
array.copy(BitConverter.GetBytes(t.ticks),1,b,4);

but getbytes(t.ticks) returns array of 8 bytes. I somehow want it to convert to 4 bytes only.

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
user3229083
  • 61
  • 1
  • 1
  • 2
  • Do you need to store just the date, or the entire date + time? – Reed Copsey Jan 23 '14 at 18:43
  • 10
    4 bytes simply isn't enough. So you need to either sacrifice range or accuracy. `DateTime` supports dates from the year 1 to 9999 in 100ns steps. This requires 8 bytes. – CodesInChaos Jan 23 '14 at 18:43
  • 1
    ...or increase the size of whatever you are storing it in. – DonBoitnott Jan 23 '14 at 18:44
  • complete datetime in 4 bytes – user3229083 Jan 23 '14 at 18:50
  • 7
    @user3229083 You can't fit a `DateTime` into 4 bytes without sacrificing *something*. It was designed to fit 8 bytes, not 4. That's a mathematical limitation you can't get around. It's like trying 4 different values in a single bit. For example if you reduce accuracy to 1 sec you get a range of around 130 years, which is the trade-off chosen by unix time stamps. – CodesInChaos Jan 23 '14 at 18:53
  • i only need date 2/1/2014 and time 11:44 in these 4 bytes, dont need seconds. cant i get it in 4 bytes. – user3229083 Jan 23 '14 at 19:07
  • What is the earliest time you need to store? November 1582? January 2014? – Dour High Arch Jan 23 '14 at 21:41
  • You could use an unsigned 32 bit unix time stamp which can represent dates from 1970 to 2100. Or you could divide the unix timestamp by 60 since you only need minutes which should give you a few thousand years of range. – CodesInChaos Jan 23 '14 at 21:43

3 Answers3

9

You can use 32 bit unix time. But be careful with year 2038 problem. You can find sample solution below. Which stores date time in 4 bytes.

        byte[] b = new byte[] { 10, 12, 12, 12 };
        DateTime now = DateTime.Now;
        var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        var csMinDate = DateTime.MinValue;
        TimeSpan tsEpoch = now - epoch;
        int passedSecods = (int)tsEpoch.TotalSeconds;
        byte[] copyBytes = BitConverter.GetBytes(passedSecods);
        Array.Copy(copyBytes, 0, b, 0, 4);
        DateTime tCompare = epoch.AddSeconds(BitConverter.ToInt32(b, 0));
cahit beyaz
  • 4,829
  • 1
  • 30
  • 25
1

Convert current time to 64 bit unix time then manually convert the 64 bit time to 32 bit time (be ready to face the Year 2038 problem.).

See SO discussion for ideas to do this:

Other References:

Community
  • 1
  • 1
George Philip
  • 704
  • 6
  • 21
-1

try with this code:

byte[] b = new byte[] { 10, 12, 12, 12 };
DateTime t = DateTime.Now;
Array.Copy(BitConverter.GetBytes(t.Ticks), 0, b, 0, 4);
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Fermin Pitol
  • 486
  • 5
  • 11