0

For an unknown reason, when I read a date from the response of execute reader I obtain a date that has 000 before the actual milliseconds. So for example in the database I have 2017-04-04 12:12:12.123455 but when I display in C# I have 2017-04-04 11:12:12.000123

while (dr.Read())
{
lst.Add(Convert.ToDateTime(dr[i]).ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
}
J. Lev
  • 307
  • 3
  • 19
  • Lets see the code. What is your query? what is your DDL? How are you displaying it? Can't read your mind over here. – Hogan Aug 17 '17 at 19:09
  • This is probably a datetime vs datetime2 problem. Datetime isn't actually precise enough for milliseconds. (I'm assuming SQL Server). – Brett Aug 17 '17 at 19:13
  • The query is SELECT * FROM TABLE... The code is the simplest possible. I'll edit. – J. Lev Aug 17 '17 at 19:13
  • Which RDBMS is this for? Please add a tag to specify whether you're using `mysql`, `postgresql`, `sql-server`, `oracle` or `db2` - or something else entirely. – marc_s Aug 17 '17 at 19:14
  • @C.Helling I meant Millionth of a seconds. I want to show 6 numbers after the ",". – J. Lev Aug 17 '17 at 19:33
  • Yes, I see now. I am guessing the other commenters are right, there probably isn't the precision in your SQL table, but impossible to know since you haven't specified the RDBMS/column type/etc. Edit: I see your comment on the other answer, you should mention that in the original question. – C. Helling Aug 17 '17 at 19:35
  • datetime values are rounded to increments of .000, .003, or .007 seconds. See https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql. See also related question https://stackoverflow.com/questions/2872444/round-net-datetime-milliseconds-so-it-can-fit-sql-server-milliseconds. – Andrey Belykh Aug 17 '17 at 21:07

1 Answers1

0

This is because the dates are a timestamp (a number of milliseconds from a determined date in the past) to have the exact amount of milliseconds you need to have the exact time in the database server and the machine you are running the c# application.

  • In the db I see the exact time and if I add enough f in the format I see the good time too, but there's 3 extras 0 after the seconds like 2017-08-08 12:12:12.123456 -> 2017-08-08 12:12:12.000123456 – J. Lev Aug 17 '17 at 19:16