前后端时间转换
一、后端
转换前:Tue Mar 22 2022 00:00:00 GMT+0800 (中国标准时间)
转换后:20220322(格式自己确定)
public String formatterDate(String date){ try { date = date.replace("GMT", "").replaceAll("\\(.*\\)", ""); SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss", Locale.ENGLISH); Date datestr = format.parse(date); return new SimpleDateFormat("yyyyMMdd").format(datestr); } catch (ParseException e) { e.printStackTrace(); } return null; }
data: 2022-03-10T16:00:00.000Z
patter:yyyyMMdd
return:20220301
private String dateFormatter(String date, String pattern) { LocalDateTime parse = LocalDateTime.parse(date,DateTimeFormatter.ISO_OFFSET_DATE_TIME).plusHours(8); return parse.format(DateTimeFormatter.ofPattern(pattern)); }
二、前端
time(){ //UTC 转指定格式日期 let date = '2018-03-07T16:00:00.000Z' console.log(moment(date).format('YYYY-MM-DD HH:mm:ss')) //GMT let localTime = moment.utc(date).toDate(); console.log(localTime) //本地日期转UTC console.log(new Date("2018-03-08 00:00:00").toISOString()) },
//获取当前的时间 dateNo(){ var myDate = new Date(); var yy = myDate.getFullYear(); // 获取完整的年份(4位,1970-????) var mm = myDate.getMonth()+1; // 获取当前月份(0-11,0代表1月) if (mm < 10) { mm = "0" + mm; } var dd = myDate.getDate(); // 获取当前日(1-31) if (dd < 10){ dd = "0" + dd; } return yy +"-"+ mm +"-"+ dd; },