A. javascript 把日期減一天
獲取當前秒數,減去86400(一天的秒數),再格式化成yyyy-MM-dd
B. 求一款js做的時間日歷控制項
這是我找到的一個js做的日歷,不知道是不是你要的,裡面有包含時間,其實在日歷裡面加時間也是比較簡單的,只要調用DATE的方法就可以了,希望對你有用。下面是代碼段,效果圖附上。
<html>
<head>
<SCRIPTLANGUAGE="JavaScript"TYPE="text/javascript">
//定義月歷函數
functioncalendar(){
vartoday=newDate();//創建日期對象
year=today.getYear();//讀取年份
thisDay=today.getDate();//讀取當前日
//創建每月天數數組
varmonthDays=newArray(31,28,31,30,31,30,31,31,30,31,30,31);
//如果是閏年,2月份的天數為29天
if(((year%4==0)&&(year%100!=0))||(year%400==0))monthDays[1]=29;
daysOfCurrentMonth=monthDays[today.getMonth()];//從每月天數數組中讀取當月的天數
firstDay=today;//復制日期對象
firstDay.setDate(1);//設置日期對象firstDay的日為1號
startDay=firstDay.getDay();//確定當月第一天是星期幾
//定義周日和月份中文名數組
vardayNames=newArray("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
varmonthNames=newArray("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
//創建日期對象
varnewDate=newDate();
//創建表格
document.write("<TABLEBORDER='0'CELLSPACING='0'CELLPADDING='2'ALIGN='CENTER'BGCOLOR='#0080FF'>")
document.write("<TR><TD><tableborder='0'cellspacing='1'cellpadding='2'bgcolor='#88FF99'>");
document.write("<TR><thcolspan='7'bgcolor='#C8E3FF'>");
//顯示當前日期和周日
document.writeln("<FONTSTYLE='font-size:9pt;Color:#FF0000'>"+newDate.getYear()+"年"+monthNames[newDate.getMonth()]+""+newDate.getDate()+"日"+dayNames[newDate.getDay()]+"</FONT>");
//顯示月歷表頭
document.writeln("</TH></TR><TR><THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>日</FONT></TH>");
document.writeln("<thbgcolor='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>一</FONT></TH>");
document.writeln("<THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>二</FONT></TH>");
document.writeln("<THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>三</FONT></TH>");
document.writeln("<THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>四</FONT></TH>");
document.writeln("<THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>五</FONT></TH>");
document.writeln("<THBGCOLOR='#0080FF'><FONTSTYLE='font-size:9pt;Color:White'>六</FONT></TH>");
document.writeln("</TR><TR>");
//顯示每月前面的"空日"
column=0;
for(i=0;i<startDay;i++){
document.writeln("<TD><FONTSTYLE='font-size:9pt'></FONT></TD>");
column++;
}
//如果是當前日就突出顯示(紅色),否則正常顯示(黑色)
for(i=1;i<=daysOfCurrentMonth;i++){
if(i==thisDay){
document.writeln("</TD><TDALIGN='CENTER'><FONTSTYLE='font-size:9pt;Color:#ff0000'><B>")
}
else{
document.writeln("</TD><TDBGCOLOR='#88FF99'ALIGN='CENTER'><FONTSTYLE='font-size:9pt;font-family:Arial;font-weight:bold;Color:#000000'>");
}
document.writeln(i);
if(i==thisDay)document.writeln("</FONT></TD>")
column++;
if(column==7){
document.writeln("<TR>");
column=0;
}
}
document.writeln("<TR><TDCOLSPAN='7'ALIGN='CENTER'VALIGN='TOP'BGCOLOR='#0080FF'>")
document.writeln("<FORMNAME='time'onSubmit='0'><FONTSTYLE='font-size:9pt;Color:#ffffff'>")
//顯示當前時間
document.writeln("當前時間:<INPUTTYPE='Text'NAME='textbox'ALIGN='TOP'></FONT></TD></TR></TABLE>")
document.writeln("</TD></TR></TABLE></FORM>");
}
</SCRIPT>
<SCRIPTLANGUAGE="JavaScript">
//初始化控制變數
vartimerID=null;
vartimerRunning=false;
//定義時間顯示函數
functionstoptime(){
if(timerRunning)
clearTimeout(timerID);
timerRunning=false;}
//定義顯示時間函數
functionshowtime(){
varnewDate=newDate();
varhours=newDate.getHours();
varminutes=newDate.getMinutes();
varseconds=newDate.getSeconds()
vartimeValue=""+((hours>12)?hours-12:hours)
timeValue+=((minutes<10)?":0":":")+minutes
timeValue+=((seconds<10)?":0":":")+seconds
timeValue+=(hours>=12)?"下午":"上午"
document.time.textbox.value=timeValue;
timerID=setTimeout("showtime()",1000);//設置超時,使時間動態顯示
timerRunning=true;}
//顯示當前時間
functionstarttime(){
stoptime();
showtime();}
</SCRIPT>
</head>
<BODYonLoad="starttime()"TEXT="#000000"TOPMARGIN="0">
<scriptlanguage="JavaScript"type="text/javascript">
calendar();//顯示月歷
</script>
</BODY>
</html>
C. 「javascript」怎樣把日期減一天
「javascript」把日期減一天,可以按照下面的進行改動:
方法一:var today=new Date();
var t=d.getTime()-1000*60*60*24;
var yesterday=new Date(t);
alert(t);
方法二:var myDate = new Date();
alert(myDate);
var date = myDate.getDate();
date = date - 1;
myDate.setDate(date);
alert(myDate);
D. 用javascript輸入某一年的第幾天,輸出這一天是幾月幾號。用for循環和函數
varyear=prompt("年");
varday=prompt("天");
if(day>0&&day<366){
varisrunnian=(year%4===0&&year%100!==0)||%400===0;
varrules=[31,isrunnian?29:28,31,30,31,30,31,31,30,31,30,31];
varresult=0;
for(vari=0;i<rules.length;i++){
if(result+rules[i]>=day){
console.log(year+"年"+(i+1)+"月"+(day-result)+"日");
return;
}
result+=rules[i];
}
}
按你圖上代碼稍微改下即可
我把變數result留著是為了便於你理解 也可以去掉直接在day上計算
for(vari=0;i<rules.length;i++){
if(rules[i]>=day){
console.log(year+"年"+(i+1)+"月"+day+"日");
return;
}
day-=rules[i];
}
E. javascript 從Date中獲取星期幾
通過getDay() 方法,可返回表襲示星期的某一天的數字。
星期中的某一天,使用本地時間。返回值是 0(周日) 到 6(周六) 之間的一個整數。
示例:
<scripttype="text/javascript">
vard=newDate()
varweekday=newArray(7)
weekday[0]="星期天"
weekday[1]="星期一"
weekday[2]="星期二"
weekday[3]="星期三"
weekday[4]="星期四"
weekday[5]="星期五"
weekday[6]="星期六"
document.write("Todayitis"+weekday[d.getDay()])//返回當前時間是星期幾
</script>
F. 求做一個JAVASCRIPT倒計時器,能計算並顯示到某一天的剩餘時間。。。
<div class="timercon">
距離活動結束還有
<div id="timer">計算中。。。</div>
</div>
<script type="text/javascript" src="timer.js"></script><!--src 中改成你實際的地址-->
從這行以下 保存為 timer.js 放在你要放的位置,不包括這行說明 然後更新上面的 src
function TimeTo(dd){
var t = new Date(dd),//取得指定時間的總毫秒數
n = new Date().getTime(),//取得當前毫秒數
c = t - n;//得到時間差
if(c<=0){//如果差小於等於0 也就是過期或者正好過期,則推出程序
document.getElementById('timer').innerHTML ='活動已經結束';
clearInterval(window['ttt']);//清除計時器
return;//結束執行
}
var ds = 60*60*24*1000,//一天共多少毫秒
d = parseInt(c/ds),//總毫秒除以一天的毫秒 得到相差的天數
h = parseInt((c-d*ds)/(3600*1000)),//然後取完天數之後的餘下的毫秒數再除以每小時的毫秒數得到小時
m = parseInt((c - d*ds - h*3600*1000)/(60*1000)),//減去天數和小時數的毫秒數剩下的毫秒,再除以每分鍾的毫秒數,得到分鍾數
s = parseInt((c-d*ds-h*3600*1000-m*60*1000)/1000);//得到最後剩下的毫秒數除以1000 就是秒數,再剩下的毫秒自動忽略即可
document.getElementById('timer').innerHTML = '<p style="margin-top:5px;"> <b>'+d+'</b> 天 <b>'+h+'</b> 小時 <b>'+m+'</b> 分</p><p class="ss"><b>'+s+'</b> 秒</p>';
//最後這句講定義好的顯示 更新到 ID為 timer的 div中
}
(function(){
window['ttt']=setInterval(function(){
TimeTo('2011/1/1 00:00:00');//定義倒計時的結束時間,注意格式
},1000);//定義計時器,每隔1000毫秒 也就是1秒 計算並更新 div的顯示
})();
從這行以上保存為 timer.js 不包括這行說明
G. javascript怎麼對日前加一年在減去一天
vargaga=newDate(),
_y=gaga.getFullYear(),
_m=gaga.getMonth(),
_d=gaga.getDate();
varnew_gaga=newDate(_y+1,_m,_d-1),
new_y=new_gaga.getFullYear(),
new_m=new_gaga.getMonth()+1,
new_d=new_gaga.getDate();
console.log(new_y+'-'+new_m+'-'+new_d)
希望可以幫到你