1

I need to convert 8 bytes from a hexadecimal value to a new datetime in C#. The function should do the same as it does in SQL Server.

I have the hexadecimal value as a string "0000AC0500000000" or a list of bytes:

["0", "0", "172", "5", "0", "0", "0", " 0 ",]

From that I need to obtain the date "2020-07-27 00:00:00:000". The convert of the SQL Server works perfectly to recover up to milliseconds with only 8 bytes. As shown convert function in SQL Server

3
  • 1
    Curious. Why do you have it in this form in the first place? Commented Sep 17, 2020 at 18:24
  • Like @Matt, I have some real questions here - mostly "what was wrong with the SQL server date/time types?" Commented Sep 17, 2020 at 19:10
  • recovery binary date from disk Commented Sep 19, 2020 at 3:02

1 Answer 1

1

Use following

            string input = "0000AC0500000000";

            int daysFrom1900 = int.Parse(input.Substring(0,8),NumberStyles.HexNumber);
            int ticksFromMidnight = int.Parse(input.Substring(8), NumberStyles.HexNumber);
            DateTime year1900 = new DateTime(1900, 1, 1);
            DateTime date = year1900.AddDays(daysFrom1900).AddTicks(ticksFromMidnight*(10/3.0));
Sign up to request clarification or add additional context in comments.

1 Comment

this is correct, but to add the milliseconds it must be done like this (sql server): DateTime date = year1900.AddDays(daysFrom1900).AddTicks(ticksFromMidnight*(10/3.0));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.