locked
HV Issue? HealthServiceDateTime, DateTime Comparison Problem RRS feed

  • Question

  • Can't get the correct DateCompare value. Returns -1 when the date variables are equal.

    In the code block below, "patient_details_HV_BasicTimestamp" is a DateTime variable. Value comes from a SQL Server table, type DateTime.

    I declared the variable as:

    DateTime dbDateofBirth = new DateTime(1900, 01, 01);

    then set it as:

    if (dbReader["DateofBirth"] != System.DBNull.Value) { dbDateofBirth = Convert.ToDateTime(dbReader["DateofBirth"]); }

    where the db value is the same as the LastUpdated.Timestamp value.

    I have no problem when the value is older or more recent, those return -1 and 1 respectively. My only problem is when the value are equal, returns -1.
                  
    Would appreciate some help; don't know if it's my code or a HV issue.

    basic_date_compare = patient_details_HV_BasicTimestamp;
    DateTime basic_timestamp = get_single_Basic.LastUpdated.Timestamp;
    result_basic = DateTime.Compare(basic_date_compare, basic_timestamp);

    Also tried this:

    result_basic = patient_details_HV_BasicTimestamp.CompareTo(basic_timestamp);

    Thanks,
    Gary
    Thursday, January 14, 2010 5:44 PM

Answers

  • Hi Gary,

    I suspect that there might be an issue with the format of TimeStamp coming from the DB. So could you please check the TimeStamp while inserting into DB. Please make sure that both the date formats should be equal while comparing timestamps.

    Regards,
    Madan kamuju
    Friday, January 15, 2010 9:34 PM

All replies

  • Hi Gary,

    I have tried to repro your issue by using below code and issue is not reproing. I am getting return value as 0 when the date's are equal by using  CompareTo method.

    Please use the below code and let me know if the issue still exists.

            HealthServiceDateTime dateTime = new HealthServiceDateTime(new DateTime(2010, 01, 13));  
            DateTime dateTimeNew = new DateTime(2010,01,13);
            int val = dateTime.CompareTo(dateTimeNew);

    Hope this helps you.

    Regards,
    Madan Kamuju
    Thursday, January 14, 2010 6:23 PM
  • Thanks Madan.

    Hmmm... I still get -1 when dates are equal.

    result_basic = patient_details_HV_BasicTimestamp.CompareTo(get_single_Basic.LastUpdated.Timestamp);

    result_basic is declared:

    int result_basic = 0;

    patient_details_HV_BasicTimestamp is a DateTime

    get_single_Basic.LastUpdated.Timestamp is a DateTime HealthServiceAudit.Timestamp.





    Thursday, January 14, 2010 7:21 PM
  • Hi Gary,

    Please try the below code for your scenario. 

            DateTime dateOld = DateTime.Now.ToUniversalTime();
            Microsoft.Health.HealthServiceAudit healthServiceAudit = new HealthServiceAudit();
            DateTime timeStamp = healthServiceAudit.Timestamp.ToUniversalTime();
            int result = 0;
            result = timeStamp.CompareTo(dateOld);

    You can use this url for reference
    http://social.msdn.microsoft.com/Forums/en-US/healthvault/thread/5c4aedf8-432c-47a5-8a92-d16bd351d667


    Hope this helps you.

    Regards,
    Madan Kamuju
    • Proposed as answer by Madan kamuju Friday, January 15, 2010 3:28 PM
    Thursday, January 14, 2010 8:05 PM
  • Thanks.

    Just to be clear, both the LastUpdated.Timestamp and DB datetime column have the same value of:

    1/13/2010 11:18:23 PM

    Though in the raw XML from X-Ray, the LastUpdated.Timestamp is displayed as:

    2010-01-13T23:18:23.46Z

    I tried converting ToUniversalTime with the scenarios below, and still don't get the correct datecompare result integer when the values are actually the same (as described above).

    Is the format of Universal Time correct when it returns a value of 14/1/2553 4:18:23 for
    1/13/2010 11:18:23 PM?

    1. Both variables converted ToUniversalTime():

    DB datetime: 14/1/2553 4:18:23
    HV Audit Timestamp: 13/1/2553 23:18:23
    DateCompare Result: 1


    2. HV LastUpdated.Timestamp converted ToUniversalTime():

    DB datetime: 13/1/2553 23:18:23
    HV Audit Timestamp: 13/1/2553 23:18:23
    DateCompare Result: 1


    3. DB datetime converted  ToUniversalTime():
    DateCompare Result: 1
    DB datetime: 14/1/2553 4:18:23
    HV Audit Timestamp: 13/1/2553 23:18:23

    Thanks again.

    Gary
    Thursday, January 14, 2010 8:48 PM
  • Hi Gary,

    Could you please check for milli second/ticks in 'Watch' (Debug mode in Visual Studio) for the complete time? The issue is not reproducible from my end. Also the one you are seeing in X-ray is right and for me while converting to Universal Time the year is not changing to '2553' and it return the expected result..!!

    Thank you,
    Anish Ravindran
    Friday, January 15, 2010 1:33 AM
  • Hi Gary,

    While debugging the DateTime.Now values may change in the CompareTo() method that is the reason it is giving result as 1. Please try to exectute the code with out debugging.

    Please let me know if the issue still exists.

    Regards,
    Madan Kamuju
    Friday, January 15, 2010 3:28 PM
  • Thanks.

    I'm not in debug mode. I'm testing with IIS.

    I have DateTime.Now.ToUniversalTime:  *displayed* in my view as:

    15/1/2553 17:27:15

    I'm using Visual Web Developer Express; I don't see how to watch the variables.

    I'm new to ASP.NET.

    Thanks.
    Friday, January 15, 2010 5:38 PM
  • Hi Gary,

    Can you please try the below code because i am not able to repro your issue.


    DateTime date1 = Convert.ToDateTime("1/13/2010 11:18:23 PM").ToUniversalTime();

    DateTime date2 = Convert.ToDateTime("1/13/2010 11:18:23 PM").ToUniversalTime();

    int result=DateTime.Compare(date1, date2);

    Please let me know the result of it. I am getting here result as 0.

    Date time format should be mm/dd/yyyy hh:mm:ss

    Hope this helps.

    Regards,
    Madan Kamuju

    Friday, January 15, 2010 6:50 PM
  • Thanks.

    I get "String was not recognized as a valid DateTime." for:

    DateTime date1 = Convert.ToDateTime("1/13/2010 11:18:23 PM").ToUniversalTime();

    Will try with DateTime.Now.

    Friday, January 15, 2010 7:53 PM
  • When I replace with DateTime.Now:

    DateTime date1 = Convert.ToDateTime(DateTime.Now).ToUniversalTime();

    DateTime date2 = Convert.ToDateTime(DateTime.Now).ToUniversalTime();

    int result=DateTime.Compare(date1, date2);


    I still get messed up year in asp view (displayed in web page):

    date1: 15/1/2553 19:56:33
    date2: 15/1/2553 19:56:33

    *but* I get the correct compare value of 0.

    So do I have my variables declared incorrectly?

    This is for db variable that is comparing to audit timestamp:

    DateTime patient_details_HV_BasicTimestamp = new DateTime(1900, 01, 01);

    then I convert to DateTime when reading:

    patient_details_HV_BasicTimestamp = Convert.ToDateTime(dr_patient_details["HV_BasicTimestamp"]);

    Is this an issue?

    Should it be declared as a string, then just convert to date time when reading from db?




                       

    Friday, January 15, 2010 8:04 PM
  • When I do:

    DateTime db_timestamp = Convert.ToDateTime(patient_details_HV_BasicTimestamp).ToUniversalTime();

    ,which is the value from the SQL DB column,

    the value of db_timestamp comes up as one day later than it actually is.

    One day later than the value in the db.

    So the value of the day in the db is 13.

    When I do the conversion above, it outputs 14 instead of 13.


    Friday, January 15, 2010 8:14 PM
  • So I think there's an issue with system datetime format.

    I tried .ToLocalTime, but still doesn't the issue of the datecompare not giving me a result value of 0 when dates are equal.
    Friday, January 15, 2010 8:48 PM
  • Hi Gary,

    I suspect that there might be an issue with the format of TimeStamp coming from the DB. So could you please check the TimeStamp while inserting into DB. Please make sure that both the date formats should be equal while comparing timestamps.

    Regards,
    Madan kamuju
    Friday, January 15, 2010 9:34 PM