1.效果
2.js
/**
* yyyy-MM--dd
* */
getNowFormatDate: function () {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
},
/**
* yyyy-MM-dd HH:mm:ss
*/
getNowFormatDate2: function () {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
//外国的月份都是从0开始的,所以+1
var month = date.getMonth() + 1;
var strDate = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
//1-9月用0补位
if(month >=1 && month <=9){
month = "0" + month;
}
//1-9日用0补位
if(strDate >=0 && strDate <= 9){
strDate = "0" + strDate;
}
//1-9日用0补位
if(hours >=0 && hours <= 9){
hours = "0" + hours;
}
//1-9日用0补位
if(minutes >=0 && minutes <= 9){
minutes = "0" + minutes;
}
//1-9日用0补位
if(seconds >=0 && seconds <= 9){
seconds = "0" + seconds;
}
//获取当前时间 yyyy-MM-dd HH:mm:ss
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + "23" + seperator2 + "59" + seperator2 + "59";
return currentdate;
},
/**
* yyyy-MM-dd HH:mm:ss
*/
getNowFormatDate3: function () {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
//外国的月份都是从0开始的,所以+1
var month = date.getMonth() + 1;
var strDate = date.getDate();
//1-9月用0补位
if(month >=1 && month <=9){
month = "0" + month;
}
//1-9日用0补位
if(strDate >=0 && strDate <= 9){
strDate = "0" + strDate;
}
//获取当前时间 yyyy-MM-dd HH:mm:ss
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " +"00" + seperator2 + "00" + seperator2 + "00";
return currentdate;
}
3.controller
try {
if (StringUtils.isNotBlank(beginTimestamp)) {
//开始时间要做成 2019-02-16 00:00:00的格式
beginTimestamp = TimeUtil.toTimestamp10(beginTimestamp + " " + "00:00:00");
}
if (StringUtils.isNotBlank(endTimestamp)) {
//结束时间要做成 2019-02-16 23:59:59的格式
endTimestamp = TimeUtil.toTimestamp10(endTimestamp + " " + "23:59:59");
}
4.TimeUtil
/**
* 时间显示转10位时间戳(yyyy-MM-dd HH:mm:ss)
* @return
*/
public static String toTimestamp10(String date) throws ParseException {
return String.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime()).substring(0,10);
}