// Assuming you have the User model and middleware from the previous examples...
// Custom function to calculate total login and logout duration for a specific date
function calculateDailyTime(userId, date, startTime, endTime) {
const startOfDay = moment(date).startOf('day'); // Start of the specified date
const endOfDay = moment(date).endOf('day'); // End of the specified date
// Retrieve user information
const user = await User.findById(userId);
// Filter login and logout times for the specified date
const dailyLoginTimes = user.loginTimes.filter(time => isSameDay(time, startOfDay));
const dailyLogoutTimes = user.logoutTimes.filter(time => isSameDay(time, startOfDay));
// Calculate total login and logout duration
const totalLoginDuration = calculateTotalDuration(
dailyLoginTimes,
dailyLogoutTimes,
endOfDay,
moment.duration(startTime), // Convert to duration
moment.duration(endTime) // Convert to duration
);
const moment = require('moment');
// Assuming you have the User model and middleware from the previous examples...
// Custom function to calculate total login and logout duration for a specific date function calculateDailyTime(userId, date, startTime, endTime) { const startOfDay = moment(date).startOf('day'); // Start of the specified date const endOfDay = moment(date).endOf('day'); // End of the specified date
// Retrieve user information const user = await User.findById(userId);
// Filter login and logout times for the specified date const dailyLoginTimes = user.loginTimes.filter(time => isSameDay(time, startOfDay)); const dailyLogoutTimes = user.logoutTimes.filter(time => isSameDay(time, startOfDay));
// Calculate total login and logout duration const totalLoginDuration = calculateTotalDuration( dailyLoginTimes, dailyLogoutTimes, endOfDay, moment.duration(startTime), // Convert to duration moment.duration(endTime) // Convert to duration );
return { date: startOfDay, totalLoginDuration: totalLoginDuration, }; }
// Example usage const userId = 'yourUserId'; const currentDate = moment(); // Today's date const startTime = { hours: 10, minutes: 0 }; // 10:00 a.m. const endTime = { hours: 17, minutes: 0 }; // 5:00 p.m.
const dailyTimeInfo = calculateDailyTime(userId, currentDate, startTime, endTime); console.log(dailyTimeInfo);
function isSameDay(date1, date2) { return moment(date1).isSame(date2, 'day'); }
function calculateTotalDuration(loginTimes, logoutTimes, endOfDay, startTime, endTime) { let totalDuration = 0;
loginTimes.forEach((loginTime, index) => { const logoutTime = logoutTimes[index] || endOfDay;
});
return totalDuration; }