1、取当前时间方法封装:
//取当前日期;
function getCurrentDate(fulldate = false, separator = ['-', ':']) {
let currentTimeObj = new Date();
let year = currentTimeObj.getFullYear();
let month = (currentTimeObj.getMonth() + 1);
month = month >= 10 ? month : '0' + month;
let day = currentTimeObj.getDate();
day = day >= 10 ? day : '0' + day;
let hour = currentTimeObj.getHours();
hour = hour >= 10 ? hour : '0' + hour;
let minute = currentTimeObj.getMinutes();
minute = minute >= 10 ? minute : '0' + minute;
let second = currentTimeObj.getSeconds();
second = second >= 10 ? second : '0' + second;
if (fulldate) {
let ymd = [year, month, day];
let his = [hour, minute, second];
return ymd.join(separator[0]) + ' ' + his.join(separator[1]);
} else {
let ymd = [year, month, day];
return ymd.join(separator[0]);
}
}
// 方法二
/**
*获取当前时间
*format=1精确到天
*format=2精确到分
*/
getCurrentDate2(format) {
var now = new Date()
var year = now.getFullYear() // 得到年份
var month = now.getMonth()// 得到月份
var date = now.getDate()// 得到日期
var hour = now.getHours()// 得到小时
var minu = now.getMinutes()// 得到分钟
var sec = now.getSeconds()// 得到秒
month = month + 1
if (month < 10) month = '0' + month
if (date < 10) date = '0' + date
if (hour < 10) hour = '0' + hour
if (minu < 10) minu = '0' + minu
if (sec < 10) sec = '0' + sec
var time = ''
// 精确到天
if (format === 1) {
time = year + '-' + month + '-' + date
} else if (format === 2) { // 精确到分
time = year + '-' + month + '-' + date + ' ' + hour + ':' + minu + ':' + sec
}
return time
},
2. 方法结合layui设置最小日期为当前日期:
var currentd=getCurrentDate(true,['-',':']); //格式化当前日期时间;
laydate.render({ //日期设置
elem: '#creattime'
,type:'datetime'
,min:currentd
});
//html:
<div class="layui-input-inline">
<input type="text" id="creattime" lay-verify="date" name="creattime" class="layui-input searchVal" placeholder="交货时间" />
</div>
3.输出打印看当前的结果:console.log(currentd)
结果:‘2019-09-12 08:54:25’;