NodeJS - 如何将分钟转换为小时和分钟格式,即 4 小时 38 分钟

问题描述 投票:0回答:2

我想以小时和分钟格式存储工作时间,即 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

我想以小时和分钟格式存储数据。

提前感谢大家的时间和贡献!!😊

javascript node.js mongodb datetime node-modules
2个回答
0
投票
    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
        }
    });
});

0
投票
const totalMinutes = Math.floor(workHoursMillis / (1000 * 60));
const totalHours = Math.floor(totalMinutes / 60);
const remainingMinutes = totalMinutes % 60;
  1. 计算总工作分钟数
  2. 将总分钟数除以 60 即可得到总小时数
  3. 计算提取小时后剩余的分钟
© www.soinside.com 2019 - 2024. All rights reserved.