- 从Mysql中取出DATE类型数据,将其转换成js中的date类型,然后显示在ElementUI中的DatePicker中
this.list.forEach(element => {
element.Student_birth = moment(element.Student_birth).format('YYYY-MM-DD');
});
其中Student_birth为date类型数据,意指学生入学时间。
这里使用的是momentjs转换时间格式,具体可以查看官网
- 将DatePicker中的date类型通过axios传送给nodejs,然后插入到mysql中
第一种方法仍然使用momentjs转换
data.Student_birth = moment(data.Student_birth).format('YYYY-MM-DD');
第二种方法参考 Stack Overflow中的解法
data.Student_birth = new Date().toJSON().slice(0, 10)
data.Student_birth = new Date().toJSON().slice(0, 19).replace('/T.*/', ' ')
data.Student_birth = new Date().toJSON().slice(0, 10)
选一个即可,比较迷的是当时这几种方法都试过但是仍然报错,但是后来又都起效了,不过在sql语句中插入的值需要加上引号:
`update student
set Student_birth='${data.Student_birth}'
where Student_id = '${data.Student_id}'`