layui 获取时间和日期(前些天,后些天,当前)
Posted 每天坚持亿点点
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了layui 获取时间和日期(前些天,后些天,当前)相关的知识,希望对你有一定的参考价值。
第一:获取当前时间
var now = new Date();
var year = now.getFullYear();//获取当前年份
var month = now.getMonth()+1;//当前月份
if(month<10)
month = "0" + month;
else
month = month;
var day = now.getDate();//当前天份
if(day<10)
day = "0" + day;
else
day = day;
var time=now.getHours();//当前小时
var getMinutes=now.getMinutes();//当前分钟
var getSeconds=now.getSeconds();//当前秒钟
var time=year+"-"+month+"-"+day+" "+time+":"+getMinutes+":"+getSeconds; //获取当前时分秒
var datatime=year+"-"+month+"-"+day; //获取当前年月日
第二:获取前些天日期
var now = new Date().getTime(); //获取毫秒数
var before = new Date(now - 2 * 86400 * 1000); //这里的2,表示2天前,例如:需要当前日期的10天前,只需要将2改成10就可以了
var beforeyear = before.getFullYear();
var beforemonth = before.getMonth()+1;
if(beforemonth<10)
beforemonth = "0" + beforemonth;
else
beforemonth = beforemonth;
var beforedate = before.getDate();//当前天份
if(beforedate<10)
beforedate = "0" + beforedate;
else
beforedate = beforedate;
var datatimeOne = beforeyear +"-" + beforemonth + "-" + beforedate;
第三:获取后些天的日期(和前些天一样,只需要把)
var now = new Date().getTime(); //获取毫秒数
var before = new Date(now + 2 * 86400 * 1000); //这里的2,表示2天后,例如:需要当前日期的10天后,只需要将2改成10就可以了
var beforeyear = before.getFullYear();
var beforemonth = before.getMonth()+1;
if(beforemonth<10)
beforemonth = "0" + beforemonth;
else
beforemonth = beforemonth;
var beforedate = before.getDate();//当前天份
if(beforedate<10)
beforedate = "0" + beforedate;
else
beforedate = beforedate;
var datatimeOne = beforeyear +"-" + beforemonth + "-" + beforedate;
以上是关于layui 获取时间和日期(前些天,后些天,当前)的主要内容,如果未能解决你的问题,请参考以下文章