撰写时间:2019年07月17日
JS将数据库查询传递过来的时间
submitTime:
将这个时间自定义为自己需要的类型:“2019-07-04 16:30:25”
传递过来的时间 var dateTime=formatDate(submitTime);
输出:dateTime=2019-07-04 16:30:25
//========================= 日期时间格式化 ======================
function formatDate(jsondate) {
if (jsondate==null || jsondate=="") {
return "";
}
try {
jsondate = jsondate.replace("/Date(", "").replace(")/", "");
} catch (e) {
jsondate=jsondate.time;
jsondate=jsondate+"";
}
if (jsondate.indexOf("+") > 0) {
jsondate = jsondate.substring(0, jsondate.indexOf("+"));
}
else if (jsondate.indexOf("-") > 0) {
jsondate = jsondate.substring(0, jsondate.indexOf("-"));
}
var date = new Date(parseInt(jsondate, 10));
var month = date.getMonth() + 1;
var day = date.getDate();
var hours=date.getHours();
var minutes=date.getMinutes();
var seconds=date.getSeconds();
return date.getFullYear()
+ "-"
+ (month < 10? '0' + month : month) + '-' +
(day < 10? '0' + day : day) + ' ' +
(hours < 10? '0' + hours : hours) + ':' +
(minutes< 10? '0' + minutes: minutes) + ':' +
(seconds< 10? '0' + seconds: seconds);
}
(2)
function formatDate(time,format='YY-MM-DD hh:mm:ss'){
var date = new Date(time);
var year = date.getFullYear(),
month = date.getMonth()+1,//月份是从0开始的
day = date.getDate(),
hour = date.getHours(),
min = date.getMinutes(),
sec = date.getSeconds();
var preArr = Array.apply(null,Array(10)).map(function(elem, index) {
return '0'+index;
});开个长度为10的数组 格式为 00 01 02 03
var newTime = format.replace(/YY/g,year)
.replace(/MM/g,preArr[month]||month)
.replace(/DD/g,preArr[day]||day)
.replace(/hh/g,preArr[hour]||hour)
.replace(/mm/g,preArr[min]||min)
.replace(/ss/g,preArr[sec]||sec);
return newTime;
}
formatDate(new Date().getTime());//2019/07/04 10:05:44
formatDate(new Date().getTime(),'YY年MM月DD日');//2019年057月04日
formatDate(new Date().getTime(),'今天是YY/MM/DD hh:mm:ss');//今天是2019/07/04 10:07:45
Java servlet根据时间生成编号的格式:yyyyMMddHHmmssSSSS
年月日时分秒毫秒(保留四位:SSSS)
long totalMilliSeconds = System.currentTimeMillis();//获取时间的一种
SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmssSSSS");//自定义日期格式
String Loannumber = format.format(totalMilliSeconds);//转换