先用getTime()转换成毫秒格式,再进行加减运算;
然后再用setTime()换成日期格式输出;
ddsetDate(ddgetDate()+AddDayCount);//获取AddDayCount天后的日期
这一句改为:
ddsetTime(ddgetTime()+AddDayCount2460601000);//获取AddDayCount天后的日期
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
td{
border:1px solid;
}
button{
margin: 5px;
}
</style>
<script>
windowonload = function(){
var cells = documentgetElementById('monitor')getElementsByTagName('td');
var clen = cellslength;
var currentFirstDate;
var formatDate = function(date){
var year = dategetFullYear()+'年';
var month = (dategetMonth()+1)+'月';
var day = dategetDate()+'日';
var week = '('+['星期天','星期一','星期二','星期三','星期四','星期五','星期六'][dategetDay()]+')';
return year+month+day+' '+week;
};
var addDate= function(date,n){
datesetDate(dategetDate()+n);
return date;
};
var setDate = function(date){
var week = dategetDay()-1;
date = addDate(date,week-1);
currentFirstDate = new Date(date);
for(var i = 0;i<clen;i++){
cells[i]innerHTML = formatDate(i==0 date : addDate(date,1));
}
};
documentgetElementById('last-week')onclick = function(){
setDate(addDate(currentFirstDate,-7));
};
documentgetElementById('next-week')onclick = function(){
setDate(addDate(currentFirstDate,7));
};
setDate(new Date());
}
</script>
</head>
<body>
<button id="last-week">上一周</button><button id="next-week">下一周</button>
<table id="monitor">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>
可以使用substring 方法提取字符串中介于两个指定下标之间的字符。
方法形式:substring(start,end)
举例:截取年月日部分的日期,代码如下:
var dateTime = new Date();
var hour = dateTimegetHours();
var min = dateTimegetMinutes();
var sec = dateTimegetSeconds();
var yy = dateTimegetFullYear();
var dd = dateTimegetDate();
var mm = dateTimegetMonth();
var time= yy + "-" + mm+1 + "-" + dd + " " + hour + ":" + min + ":" + sec;
consolelog(timesubstring(0,10));
截取结果如下图所示:
todayDate = new Date();
date = todayDategetDate();
month= todayDategetMonth() +1;
year= todayDategetYear();
documentwrite("今天是")
documentwrite("<br>")
if(navigatorappName == "Netscape")
{
documentwrite(1900+year);
documentwrite("年");
documentwrite(month);
documentwrite("月");
documentwrite(date);
documentwrite("日");
documentwrite("<br>")
}
if(navigatorappVersionindexOf("MSIE") != -1)
{
documentwrite(year);
documentwrite("年");
documentwrite(month);
documentwrite("月");
documentwrite(date);
documentwrite("日");
}
if (todayDategetDay() == 5) documentwrite("星期五")
if (todayDategetDay() == 6) documentwrite("星期六")
if (todayDategetDay() == 0) documentwrite("星期日")
if (todayDategetDay() == 1) documentwrite("星期一")
if (todayDategetDay() == 2) documentwrite("星期二")
if (todayDategetDay() == 3) documentwrite("星期三")
if (todayDategetDay() == 4) documentwrite("星期四")
以上就是关于js获取日期:前天,昨天,今天,明天,后天全部的内容,包括:js获取日期:前天,昨天,今天,明天,后天、js点击上一周获取上N周的日期和对应的星期,击下一周获取下N周的日期和对应的星期、JS 如何截取日期部分呢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)