我在TypeScript Node中创建了一个与Typescript react前端通信的后端。用Mongoose创建用户的模型架构,每个用户都有一个:
我需要将日期格式更改为“ DD-MM-YYYY”,以便在更新/编辑用户时可以自动以defaultValue读取它。
user.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 }
Edit.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”
答案 0 :(得分:0)
尝试一下
db.getCollection('test').aggregate(
[
{
$project: {
newDate: {
$dateToString: {
format: '%d-%m-%Y',
date: '$date'
}
}
}
}
]
)