milliseconds in Date

Posted on 2006-04-04
Last Modified: 2008-01-09

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.
Question by:vishnunarayan
    LVL 7

    Accepted Solution

    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"]);
    LVL 12

    Assisted Solution

    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)?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now