I have created a backend in TypeScript Node that communicates with a typescript react frontend. The model schemas for users are created with Mongoose and each user have a:
- First Name
- Last Name
- Date of Birth
- Creation time
I need to change the format of the date to "DD-MM-YYYY" so it can be automatically read in a defaultValue when a user is updated/edited.
Backend
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 }
Frontend
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>
The front show all the information of the user when I try to update it expect for the Date because the format is wrong.
It uses "2020-02-26T10:22:19.018Z" It needs to use: "26-02-2020"