我想以小时和分钟格式存储工作时间,即 4 小时 38 分钟。
我通过减去签入时间和签出(当前)时间来转换为总分钟数。
工作时间将在员工退房时计算。
我使用 MongoDB 作为数据库。
让我更准确地说,当员工结帐时,我想以小时和分钟格式计算总工作时间
attendanceController.js
-----------------------
exports.checkOutAttendance = catchAsync(async (req, res, next) => {
const employee = await User.findById(req.user?.id);
const currentDate = format(new Date(), "dd-MM-yyyy");
if (!employee) {
return next(new AppError("Employee not found or not LoggedIn", 404));
}
const existingAttendance = await Attendance.findOne({
employee: employee._id,
date: currentDate
});
if (!existingAttendance || !existingAttendance.checkInTime) {
return next(new AppError("You have not checkedIn for today", 400));
}
const checkInTime = existingAttendance.checkInTime;
const checkOutTime = Date.now();
const workHoursMillis = checkOutTime - checkInTime;
const totalMinutes = Math.floor(workHoursMillis / (1000 * 60));
// const totalHours =
const checkedOut = await Attendance.findByIdAndUpdate(existingAttendance._id, {
checkOutTime: checkOutTime,
workMinutes: totalMinutes,
notes: "CheckedOut Successfully"
});
res.status(200).json({
status: 'success',
data: {
data: checkedOut
}
});
});
attendanceModel.js
------------------
const mongoose = require("mongoose")
const attendanceSchema = new mongoose.Schema({
employee: {
type: mongoose.Schema.ObjectId,
ref: "Employee"
},
date: {
type: String,
unique: false
},
checkInTime: {
type: Date
},
checkOutTime: {
type: Date
},
workHours: {
type: Number
},
notes: {
type: String
}
})
const Attendance = mongoose.model("Attendance", attendanceSchema)
module.exports = Attendance
我想以小时和分钟格式存储数据。
提前感谢大家的时间和贡献!!😊
const attendanceSchema = new mongoose.Schema({
employee: {
type: mongoose.Schema.ObjectId,
ref: "Employee"
},
date: {
type: String,
unique: false
},
checkInTime: {
type: Date
},
checkOutTime: {
type: Date
},
workHours: { // Stores the total hours
type: Number,
default: 0
},
workMinutes: { // Stores the leftover minutes after hours are calculated
type: Number,
default: 0
},
notes: {
type: String
}
})
const Attendance = mongoose.model("Attendance", attendanceSchema);
module.exports = Attendance;
修改考勤架构
更新结账功能
exports.checkOutAttendance = catchAsync(async (req, res, next) => {
const employee = await User.findById(req.user?.id);
const currentDate = format(new Date(), "dd-MM-yyyy");
if (!employee) {
return next(new AppError("Employee not found or not LoggedIn", 404));
}
const existingAttendance = await Attendance.findOne({
employee: employee._id,
date: currentDate
});
if (!existingAttendance || !existingAttendance.checkInTime) {
return next(new AppError("You have not checkedIn for today", 400));
}
const checkInTime = existingAttendance.checkInTime;
const checkOutTime = Date.now();
const workHoursMillis = checkOutTime - checkInTime;
const totalMinutes = Math.floor(workHoursMillis / (1000 * 60));
const hours = Math.floor(totalMinutes / 60); // Calculate total hours
const minutes = totalMinutes % 60; // Calculate remaining minutes
const checkedOut = await Attendance.findByIdAndUpdate(existingAttendance._id, {
checkOutTime: checkOutTime,
workHours: hours,
workMinutes: minutes,
notes: "CheckedOut Successfully"
}, {new: true}); // Ensure that the updated document is returned
// Respond with success or further information
res.status(200).json({
status: 'success',
data: {
attendance: checkedOut
}
});
});
const totalMinutes = Math.floor(workHoursMillis / (1000 * 60));
const totalHours = Math.floor(totalMinutes / 60);
const remainingMinutes = totalMinutes % 60;