英文:
Calculate difference between two times on Qualtrics
问题
被调查者记录开始时间(上床时间)和结束时间(起床时间),分别使用flatpickr的时间选择器在不同的问题中记录。响应以以下格式记录,例如,上床时间是11:00 PM,起床时间是8:00 AM。
我需要计算上床时间 = (起床时间 - 上床时间)以分钟为单位。
要计算上床时间,我尝试了以下方法:
Qualtrics.SurveyEngine.addOnload(function()
{
/*在页面加载时运行的JavaScript代码*/
});
Qualtrics.SurveyEngine.addOnReady(function()
{
// 获取开始时间和结束时间问题的值
var timeToBed = "${q://QID4/ChoiceTextEntryValue}";
var timeOutOfBed = "${q://QID12/ChoiceTextEntryValue}";
// 创建开始时间和结束时间的Date对象
var timeToBedDate = new Date("1/1/2000 " + timeToBed);
var timeOutOfBedDate = new Date("1/1/2000 " + timeOutOfBed);
// 检查结束时间是否在开始时间之前(即时间跨越不同的天)
if (timeOutOfBed < timeToBed) {
timeOutOfBedDate.setDate(timeOutOfBedDate.getDate() + 1);
}
// 计算分钟差
var timeInBedMinutes = (timeOutOfBedDate - timeToBedDate) / 1000 / 60;
// 将差值保存为嵌入数据变量
Qualtrics.SurveyEngine.setEmbeddedData("timeInBed", timeInBedMinutes);
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*在页面卸载时运行的JavaScript代码*/
});
英文:
Respondents record a start time (time to bed) and and end time (time out of bed) in separate questions using flatpickr’s time picker. The responses are recorded in the format, for example, 11:00 PM and 8:00 AM, for start time and end time respectively.
I need to calculate time in bed = (time out of bed - time to bed) in minutes.
To calculate time in bed, I attempted the following:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
});
Qualtrics.SurveyEngine.addOnReady(function()
{
// get the values of the start and end time questions
var timeToBed = "${q://QID4/ChoiceTextEntryValue}";
var timeOutOfBed = "${q://QID12/ChoiceTextEntryValue}";
// create Date objects for the start and end times
var timeToBedDate = new Date("1/1/2000 " + timeToBed);
var timeOutOfBedDate = new Date("1/1/2000 " + timeOutOfBed);
// check if the end time is before the start time (i.e. the times are on different days)
if (timeOutOfBed < timeToBed) {
timeOutOfBedDate.setDate(timeOutOfBedDate.getDate() + 1);
}
// calculate the difference in minutes
var timeInBedMinutes = (timeOutOfBedDate - timeToBedDate) / 1000 / 60;
// save the difference as an embedded data variable
Qualtrics.SurveyEngine.setEmbeddedData("timeInBed", timeInBedMinutes);
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
答案1
得分: 0
如果您想使用Moment.js,可以尝试使用以下代码:
const timeToBedDate = moment("11:15 PM", "HH:mm A"); // HH:mm A 是您传递的时间的格式,如果需要特定日期,请更改一些内容
const timeOutOfBedDate = moment("8:05 AM", "HH:mm A");
console.log(timeToBedDate.format()); // 2023-06-20T23:15:00+02:00
console.log(timeOutOfBedDate.format()); // 2023-06-21T08:05:00+02:00
if (timeOutOfBedDate.isBefore(timeToBedDate)) timeOutOfBedDate.add(1, 'day'); // 如果起床时间在就寝时间之前,则添加一天
const timeInBedMinutes = timeOutOfBedDate.diff(timeToBedDate, 'minutes'); // 计算两个日期之间的分钟差
console.log(timeInBedMinutes); // 530分钟
基本上,我从时间字符串创建了两个Moment.js对象,并指定了我向它们传递的格式(使用 HH:mm A
)。如果日期顺序颠倒,我会添加一天。最后,我使用 diff
函数来计算两个日期之间的分钟差,如果您还想要小数部分,可以插入第三个布尔参数。
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
英文:
If you want to use moment you can try using this code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const timeToBedDate = moment("11:15 PM", "HH:mm A"); // HH:mm A is the format of the time you pass, if you need a specific date it's to change something
const timeOutOfBedDate = moment("8:05 AM", "HH:mm A");
console.log(timeToBedDate.format()); // 2023-06-20T23:15:00+02:00
console.log(timeOutOfBedDate.format()); // 2023-06-21T08:05:00+02:00
if (timeOutOfBedDate.isBefore(timeToBedDate)) timeOutOfBedDate.add(1, 'day'); // Add one day if the out of bed is before go to bed
const timeInBedMinutes = timeOutOfBedDate.diff(timeToBedDate, 'minutes'); // Calculate the difference between two date
console.log(timeInBedMinutes); // 530 minutes
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
<!-- end snippet -->
Basically I create two moment objects from the time string, and specify what format I'm passing it to them (with HH:mm A
). I add a day in case the dates are reversed (as you did). At the end I use the diff
function to calculate the minutes from one date to the other, if you also want decimals you have to insert a third boolean parameter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论