首页 > 解决方案 > 在 TypeScript 和 MongoDB 中仅获取没有时间的日期

问题描述

我在 TypeScript 中有以下代码

const EmployeeDetailsSchema: mongoose.Schema = new mongoose.Schema({
  employeeId: { type: String },
  advance: {
    lastAdvanceClosedOn: { type: String },
    pending: { type: String },
    nextVacation: { type: String },
    IquamaStatus: { type: String },
    allowedAdvance: { type: Number },
    duration: { type: String },
    requiredAdvance: { type: Number },
    adDate: { type: Date, default: new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate()) }}
}
);

在我的数据库表中,adDate如下所示;

日期:2020-03-12T00:00:00.000+00:00

但我想要如下输出;

添加日期 : 12-03-2020

如何在不将“类型”更改为字符串的情况下实现这一点

标签: javascriptmongodbtypescripttimestamp

解决方案


使用 moment.js

moment(<your Date goes here>).format("DD-MM-YYYY")

然后传递值


推荐阅读