Java
直接复制后封装在一个工具类中,要用的时候,直接传参就可以使用的那种,先说时间转时间戳的
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式的声明
Date time = new Date(); // 获取当前时间
String formattime = simpledateformat.format(time);// 格式化时间
long timestamp = time.getTime() / 1000L; //时间转换为时间戳
return timestamp;
这个是当前时间的,如果是前端传过来的,更改一下里面的数据即可,很简单
这里是时间戳转换为时间的
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long timestamp = time.getTime() / 1000L;
Date timesimple = new Date(timestamp * 1000L); //先转换为普通时间
String truetime= simpledateformat.format(timesimple ); //按照格式转为需要时间
JavaScript
时间转时间戳
一样的很简单,在前端处理,直接调用Date中的方法
let date = new Date(传入格式化好的时间); //格式为"yyyy-MM-dd HH:mm:ss"
let time = date.getTime();
return time;
时间戳转时间
let timestart = new Date(时间戳);
let time = timestart.toLocaleDateString().replace(/\//g,"-") + " ");
return time;