[英]Format Date in Mongoose
我在 TypeScript Node 中創建了一個后端,它與 typescript react 前端進行通信。 用戶的模型模式是用 Mongoose 創建的,每個用戶都有:
我需要將日期的格式更改為“DD-MM-YYYY”,以便在用戶更新/編輯時可以在 defaultValue 中自動讀取它。
用戶.ts
import * as mongoose from 'mongoose';
interface IUser {
_id: string;
firstName: string;
lastName: string;
email: string;
dob: Date; // Date of Birth
created: Date;
}
const UserSchema = new mongoose.Schema({
firstName: String,
lastName: String,
email: String,
dob: Date, // Date of Birth
created: Date,
});
const UserModel = mongoose.model('User', UserSchema);
export { UserModel, IUser }
編輯.tsx
<div className="form-group col-md-12">
<label htmlFor="dob"> Date of Birth </label>
<input
type="date"
id="dob"
defaultValue={moment(this.state.user.dob).format("DD-MM-YYYY")}
onChange={(e) => this.handleInputChanges(e)}
name="dob"
className="form-control"
placeholder="Enter customer's date of birth"
/>
</div>
當我嘗試更新它期望的日期時,前面會顯示用戶的所有信息,因為格式錯誤。
它使用“2020-02-26T10:22:19.018Z” 它需要使用:“26-02-2020”
嘗試這個
db.getCollection('test').aggregate(
[
{
$project: {
newDate: {
$dateToString: {
format: '%d-%m-%Y',
date: '$date'
}
}
}
}
]
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.