Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1655
  • Last Modified:

milliseconds in Date


I have a class "date.cs" in which a property modifieddate is of the type DateTime.

In another class i am trying to read a datevalue from a datagridview which is populated from the database.

The database table has milliseconds in all its date columns.
When i try to read the date value from the datagridview and assign it to the modifieddate of the date.cs class the milliseconds value doesnt get assigned because of which i am facing errors.

How do i solve this problem of fetching the milliseconds value.

My piece of code

string modDate = Convert.Todatetime(dtv.rows[0].cells["datecolumn"].value.tostring("yyyy-MM-dd HH:mm:ss:fff"));

Datetime getDate = DateTime.ParseExact(modDate,"yyyy-MM-dd HH:mm:ss:fff",culture);
where culture :
CultureInfo culture = System.Threading.Thread.CurrentThread.CurrentCulture;

and i assign this getDate to the modifieddate of the date.cs file.
At this time i am loosing the milliseconds part.
2 Solutions
Not sure what is going wrong here, because i have no example of the contents of "datecolumn".   But the conversion from string to date to string to date looks fishy.  Have you tried simply? Which will try to convert the string in "datecolumn" to a date using the current culture.  

DateTime getDate = Convert.ToDateTime(dtv.rows[0].cells["datecolumn"]);
mjmarlow is right, there are a lot of conversions going on here, first you are doing datetime to string from the dataview, then that back to datetime using Convert.ToDateTime, then back to a string into modDate, then back to a datetime in getDate using ParseExact. Apart from anything else, this will stink for performance. Is there a reason you need to do it like this?

However, are you saying that getDate has the right datetime, including milliseconds and that you are then losing the milliseconds when you do FileInfo finf = new FileInfo("date.cs"); finf.ModifiedDate = getDate;? Does getDate definitely have the right milliseconds (you have checked them using the debugger)?


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now