cerner/smart-on-fhir-tutorial

Example app misinterprets patient date of birth

abanks-asc opened this issue · 2 comments

When the example app gets the JSON about the patient, it has a bug in translating the JSON of the birthdate (say, "2012-02-19") into a local date ("2/19/2012").

It's off by one day. The JSON may say "2012-02-19," but the date displayed says "2/18/2012". This is because the example app just feeds the JSON into a JavaScript Date object (https://github.com/cerner/smart-on-fhir-tutorial/blob/gh-pages/example-smart-app/src/js/example-smart-app.js#L30)

Note I am not talking about being off by one month. You've already adjusted the month for January being 0 not 1. I am talking about being off by one day.

The problem is that when you initialize the Date object with a date string, it interprets it as UTC time zone, not local time zone. But then when you run .getDate(), it gives you local time zone. So "2012-02-19" is interpreted as a UTC date of midnight on 2/19, which when localized to the US will come across as some time the day before.

"parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged" --- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Currently:

var dob = new Date(patient.birthDate);

Suggested (use the multi-argument version of Date, something like this)

var dob = patient.birthDate.split('-');
dob[1]--; // in JavaScript, January is 0
dob = new Date(dob[0], dob[1], dob[2]);

Thanks for logging this @abanks-asc! This has been fixed in PR #54.