首页 > 解决方案 > 如何从猫鼬中的日期类型中检索毫秒?

问题描述

我正在尝试做一个简单的应用程序,由于我是初学者,我正在努力检索保存在我的猫鼬模式中的日期并将其转换为毫秒。

首先,在我的前端,我想保存输入中的日期:IMAGE

这是它在我的数据库中的保存方式:图像 格式:2020-01-01T00:00:00.000+00:00

当我返回前端计算月份时,我希望以毫秒为单位。如果我将数据类型更改为“数字”,它就可以工作。但是,我不能再从输入中插入日期类型。

这是我的架构:

const mongoose = require("mongoose");

const EmployeeSchema = new mongoose.Schema({
  employeeName: {
    type: String,
    required: true,
  },
  employeeShift: {
    type: String,
    required: true,
  },
  employeeStartDay: {
    type: Date,
    required: true,
  },
});

const Employee = mongoose.model("Employee", EmployeeSchema);
module.exports = Employee;

我的前端:

{employeeList.map((val, key) => {
        const month = 1000 * 60 * 60 * 24 * 30;
        const startDate = val.employeeStartDay;
        const finalDate = date.getTime();

        const months = Math.floor(-(startDate - finalDate) / month);
        const workingDaysToBeUsed = Math.floor(months * 1.166);

        return (
          <div>
            <table>
              <tr>
                <th>Specialist</th>
                <th>Schedule</th>
                <th>Start Day</th>
                <th>months passed</th> {/*months passed*/}
                <th>Days used</th>
                <th>Days accumulated</th>
              </tr>
              <tr>
                <td>{val.employeeName}</td>
                <td>{val.employeeShift}</td>
                <td>{startDate}</td>
                <td>{months}</td>
                <td>{}</td>
                <td>{workingDaysToBeUsed}</td>
              </tr>
            </table>
          </div>
        );
      })}

如果我尝试 startDate.getTime() 我收到 startDate.getTime 不是函数的错误消息,这是我不明白的。

我会很感激一些帮助。

标签: node.jsreactjsmongodbmongoose

解决方案


推荐阅读