如何使用 Healthkit 计算睡眠时间

How to calculate sleep hours with Healthkit

我正在尝试创建一个包含睡眠时间的图表,我从 HealthKit 获取数据,但我不确定如何计算,我收到的 SleepData 是这样的:

>>>>>>> SLEEP [{"endDate": "2021-12-07T09:07:00.000-0300", "sourceId": "com.apple.Health", "sourceName": "Health", "startDate": "2021-12-06T22:07:00.000-0300", "value": "INBED"}]

我在我的应用程序中使用时刻来获取一些日期,但我不确定如何计算以获取开始日期和结束日期之间的时间

获取此信息的代码是 healthKit 标准

AppleHealthKit.getSleepSamples(options, (callbackError, results) => {
        setSleepAnalysis(results);
        console.log('>>>>>>> SLEEP', results);

感谢您的帮助

如果你想在几分钟内得到:

function getMinutesBetweenDates(startDate, endDate) {
    var diff = endDate.getTime() - startDate.getTime();
    return (diff / 60000);
}

const start = Date.parse(yourStartDate);
const end = Date.parse(yourEndDate);

getMinutesBetweenDates(start, end); <== your result