getting rid of milliseconds in DateTime

I've got this situation where I get dated records from an Internet server and logically they should be stamped at 2-sec intervals. Most of them are. However, some of the records have an "update" meaning. They carry one or two fields that are supposed to be used as a correction to the field values of a record to have arrived before. The rest of the fields have zeroes. Those "nonstandard" or out of sequence records stamped by "real time." What it means is they have milliseconds.

Milliseconds are superfluous to my task and I've been trying to get rid of them. It does not appear to be easy. What I did first was to subtract them like this:

DateTime dateTime;

dateTime = dateTime.AddMilliseconds (-dateTime.Millisecond);

The problem is that the time stamp portion after the last decimal point that separates seconds from milliseconds has four digits instead of three. In other words I have to deal with microseconds as well. The operation above does not make any improvement in my situation since I end up with a time stamp like this: 12:17:28.0004

I still cannot use the field value to match this record with the previous one where the value belongs.

I also have another situation where the DateTime is calculated from a Double value x of a plot with abscissa corresponding to DateTime. I get "precise" result which I actually do not need with accuracy to hundreds of miscoseconds.

What can be done about it

I also want to add that I actually need a rather fast algorithm. Although the stream of records from my description may not appear to be too overwhelming in fact it might be. First, some records have near duplicates stamped with an even number of seconds and I have to compress them into one, then the stream is actually multiplied by the number of companies whose information is detailed in the records. At production time I may expect about one new record per millisecond or so.

Many thanks.




Answer this question

getting rid of milliseconds in DateTime

  • cplusplus1

     nobugz wrote:
    It is not clear what you ended up using.  Please mark the thread as answered.

    I am using DateTime values that are rounded up (truncated if you wish) to 2 seconds or rather even seconds.



  • Rollin561

    It is not clear what you ended up using. Please mark the thread as answered.


  • Blkbird

     CommonGenius.com wrote:
    The issue is not in displaying the time, it is in comparing two DateTime instances. However, that brings up a good point: instead of using equality to compare the two DateTimes, you could subtract them to get a TimeSpan instance, and see if that TimeSpan instance is between 0 and 2 seconds.

    My primary concern is using the DateTime field in DataTable.Select ("dateTimed = #"+dateTime.ToString()+"#"); command (dateTimed is a column name). If I do not round the dateTime value down to even seconds then it is hard to expect a match. Of course I can use nobugz idea but it would complicate the statement, I think.

    Actually your idea is already hard at work.

    Thanks.



  • Gumbatman

    The C# General thread should be limited to questions about the C# language and language-specific IDE features in Visual Studio. Moving this question to the .NET Base Class Library forum.

    A faster way would be to work with the Ticks property:



    DateTime dtNew = dt.AddTicks(-(dt.Ticks % 10000000));

    The Ticks property returns the number of 100-nanosecond increments in the instance. Since you only care about seconds, you can take the remainder after dividing the number of ticks by ten million (one billion divided by 100), and subtract that remainder from the value of the instance. This will be faster than creating a new instance using the individual year, month, day, hour, minute, and second properties, since accessing each one of those properties requires the DateTime object to calculate the correct value from the number of ticks.

    I have not tested the code above, but it should give you the idea.



  • MrSock

    The issue is not in displaying the time, it is in comparing two DateTime instances. However, that brings up a good point: instead of using equality to compare the two DateTimes, you could subtract them to get a TimeSpan instance, and see if that TimeSpan instance is between 0 and 2 seconds.

  • xRuntime

    You're right, since you care about every other second you would modulo by 20 million instead of 10.

    Don't worry about posting in the wrong place. The number of different forums is growing rapidly and it can be hard to sort through them all. I moved it because since it is not a C# specific question, other people who are looking for answers are more likely to find this post in the base class library forum.



  • Cobaia

    I don't really get it, I might be missing something. If you don't want to see the milliseconds then don't display them:

    string s = DateTime.Now.ToString("hh:mm:ss");


  • Steven Rosenthal

    Hi,

    A possible solution is to create a new DateTime object by copying everything except the milliseconds part:

    DateTime dt = new DateTime(2007, 01, 01, 10, 0, 0, 1);
    Console.WriteLine(dt.ToString("yyyy-mm-dd HH:MM:ss FFFFFFF"));

    DateTime
    dtNew = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
    Console.WriteLine(dtNew.ToString("yyyy-mm-dd HH:MM:ss FFFFFFF"));

    Console
    .ReadLine();

    Greetz,

    Geert

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog



  • Alastair Q

    Geert Verhoeven wrote:

    Hi,

    A possible solution is to create a new DateTime object by copying everything except the milliseconds part:

    DateTime dt = new DateTime(2007, 01, 01, 10, 0, 0, 1);
    Console.WriteLine(dt.ToString("yyyy-mm-dd HH:MM:ss FFFFFFF"));

    DateTime
    dtNew = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
    Console.WriteLine(dtNew.ToString("yyyy-mm-dd HH:MM:ss FFFFFFF"));

    Console
    .ReadLine();

    Greetz,

    Geert

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thank you Geert. I had this in mind. I kind of hoped there might be a faster way:). If no better solution is on the horizon (I will wait for other posts) then your suggestion will be the default solution.



  • mu2

    CommonGenius.com wrote:

    The C# General thread should be limited to questions about the C# language and language-specific IDE features in Visual Studio. Moving this question to the .NET Base Class Library forum.

    A faster way would be to work with the Ticks property:



    DateTime dtNew = dt.AddTicks(-(dt.Ticks % 10000000));

    The Ticks property returns the number of 100-nanosecond increments in the instance. Since you only care about seconds, you can take the remainder after dividing the number of ticks by ten million (one billion divided by 100), and subtract that remainder from the value of the instance. This will be faster than creating a new instance using the individual year, month, day, hour, minute, and second properties, since accessing each one of those properties requires the DateTime object to calculate the correct value from the number of ticks.

    I have not tested the code above, but it should give you the idea.

    Yep, it seems to be the right direction. I've been thinking about ticks myself but knowing no detail was too timid to explore. As I can see I can actually make the seconds even in one stroke if I divide by twenty million, right In reality I care about every other second.

    Thanks much. I appreciate it.

    Speaking about placing my post in the wrong forum originally, it was the best I could do. The forums are numerous and being not an insider it is hard for me to find the right place anyway. So I rely on people like yourselves for guidance. I do not mind the bump/dump as log as I get an answer I need.



  • getting rid of milliseconds in DateTime