getTime:function(){
var date = new Date(),
year = date.getFullYear(),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
month >= 1 && month <= 9 ? (month = "0" + month) : "";
day >= 0 && day <= 9 ? (day = "0" + day) : "";
var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
return timer;
}
另外,也能够将从数据库获取到的datetime数据类型的字段做为参数传入该方法,如:io
getTime:function(time){
var date = new Date(time),
year = date.getFullYear(),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
month >= 1 && month <= 9 ? (month = "0" + month) : "";
day >= 0 && day <= 9 ? (day = "0" + day) : "";
var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
return timer;
}
参考:https://www.shangmayuan.com/a/070e67fdfab6425eb1eb073c.html
这篇博客介绍了JavaScript中用于格式化日期的函数,包括获取年、月、日、小时、分钟和秒的方法,并展示了如何处理位数不足的情况。此外,还讨论了如何接收数据库返回的datetime数据类型并进行格式化。对于前端开发人员来说,这是一个实用的日期处理参考。
1402





