最近写项目时接口请求的参数年月日的时间格式需要转化一下,然后大脑短路,突然忘记怎么处理的了,这里复习一下,做个笔记
yyyy-mm-dd转化为yyyymmdd
var date = "2023-01-02";
var newDate = date.replace(/-/g, "");//结果:20230102
如果只要年月
var date = "2023-01-02";
var index = date.lastIndexOf("-");
var newDate = date.slice(0, index).replace("-", "");//结果:202301
yyyymmdd转化为yyyy-mm-dd
var date = "20230102";
var year = date.substring(0, 4);
var month = date.substring(4, 6);
var day = date.substring(6, 8);
var newDate = `${year}-${month}-${day}`;//结果:2023-01-02
就这些,很简单的js处理
标签:02,01,转化,var,2023,date,格式,年月日,newDate From: https://www.cnblogs.com/s272/p/18112860