导航:首页 > 编程语言 > js设定日期

js设定日期

发布时间:2024-11-15 19:55:02

A. 怎么用js给一个文本输入框赋值为当前日期

怎么用JS给一个文本输入框赋值为当前日期
document.getElementById("文本框ID").value=new Date();
如果你没有使用任何前端框架,内且需要重手写,那容么
1、如果需要设置日期的格式,就需要手动拼装日期了,如document.getElementById("文本框ID").value=(new Date().getFullYear())+"-"+(new Date().getMonth()+1)+"-"+(new Date().getDate());
2、如果要实时显示,就需要setTimeout或者seTinterval不停的赋值。

B. js 中问下如何定义一个日期变量并获得这个变量的年份

1、判断用户输入的是否有效日期
这个找一个日期校验函数即可
2、读取日期的年度,用函数getYear()或者getFullYear()

C. js如何创建那个日期选择框啊谢谢了,大神帮忙啊

给你推荐一个间断兼容性比较好JS代码! <html> <head> <title>Js日期选择器并自动加入到输入框中</title> <meta http-equiv="content-Type" content="text/html;charset=gb2312"> <script type="text/javascript"> var gMonths=new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"); var WeekDay=new Array("日","一","二","三","四","五","六"); var strToday="今天"; var strYear="年"; var strMonth="月"; var strDay="日"; var splitChar="-"; var startYear=2000; var endYear=2050; var dayTdHeight=12; var dayTdTextSize=12; var gcNotCurMonth="#E0E0E0"; var gcRestDay="#FF0000"; var gcWorkDay="#444444"; var gcMouseOver="#79D0FF"; var gcMouseOut="#F4F4F4"; var gcToday="#444444"; var gcTodayMouseOver="#6699FF"; var gcTodayMouseOut="#79D0FF"; var gdCtrl=new Object(); var goSelectTag=new Array(); var gdCurDate=new Date(); var giYear=gdCurDate.getFullYear(); var giMonth=gdCurDate.getMonth()+1; var giDay=gdCurDate.getDate(); function $(){var elements=new Array();for(var i=0;i<arguments.length;i++) {var element=arguments[i];if(typeof(arguments[i])=='string'){element=document.getElementById(arguments[i]);}if(arguments.length==1){return element;}elements.Push(element);}return elements;} Array.prototype.Push=function(){var startLength=this.length;for(var i=0;i<arguments.length;i++){this[startLength+i]=arguments[i];}return this.length;} String.prototype.HexToDec=function(){return parseInt(this,16);} String.prototype.cleanBlank=function(){return this.isEmpty()?"":this.replace(/\s/g,"");} function checkColor(){var color_tmp=(arguments[0]+"").replace(/\s/g,"").toUpperCase();var model_tmp1=arguments[1].toUpperCase();var model_tmp2="rgb("+arguments[1].substring(1,3).HexToDec()+","+arguments[1].substring(1,3).HexToDec()+","+arguments[1].substring(5).HexToDec()+")";model_tmp2=model_tmp2.toUpperCase();if(color_tmp==model_tmp1 ||color_tmp==model_tmp2){return true;}return false;} function $V(){return $(arguments[0]).value;} function fPopCalendar(evt,popCtrl,dateCtrl){evt.cancelBubble=true;gdCtrl=dateCtrl;fSetYearMon(giYear,giMonth);var point=fGetXY(popCtrl);with($("calendardiv").style){left=point.x+"px";top=(point.y+popCtrl.offsetHeight+1)+"px";visibility='visible';zindex='99';position='absolute';}$("calendardiv").focus();} function fSetDate(iYear,iMonth,iDay){var iMonthNew=new String(iMonth);var iDayNew=new String(iDay);if(iMonthNew.length<2){iMonthNew="0"+iMonthNew;}if(iDayNew.length<2){iDayNew="0"+iDayNew;}gdCtrl.value=iYear+splitChar+iMonthNew+splitChar+iDayNew;fHideCalendar();} function fHideCalendar(){$("calendardiv").style.visibility="hidden";for(var i=0;i<goSelectTag.length;i++){goSelectTag[i].style.visibility="visible";}goSelectTag.length=0;} function fSetSelected(){var iOffset=0;var iYear=parseInt($("tbSelYear").value);var iMonth=parseInt($("tbSelMonth").value);var aCell=$("cellText"+arguments[0]);aCell.bgColor=gcMouseOut;with(aCell){var iDay=parseInt(innerHTML);if(checkColor(style.color,gcNotCurMonth)){iOffset=(innerHTML>10)?-1:1;}iMonth+=iOffset;if(iMonth<1){iYear--;iMonth=12;}else if(iMonth>12){iYear++;iMonth=1;}}fSetDate(iYear,iMonth,iDay);} function Point(iX,iY){this.x=iX;this.y=iY;} function fBuildCal(iYear,iMonth){var aMonth=new Array();for(var i=1;i<7;i++){aMonth[i]=new Array(i);}var dCalDate=new Date(iYear,iMonth-1,1);var iDayOfFirst=dCalDate.getDay();var iDaysInMonth=new Date(iYear,iMonth,0).getDate();var iOffsetLast=new Date(iYear,iMonth-1,0).getDate()-iDayOfFirst+1;var iDate=1;var iNext=1;for(var d=0;d<7;d++){aMonth[1][d]=(d<iDayOfFirst)?(iOffsetLast+d)*(-1):iDate++;}for(var w=2;w<7;w++){for(var d=0;d<7;d++){aMonth[w][d]=(iDate<=iDaysInMonth)?iDate++:(iNext++)*(-1);}}return aMonth;} function fDrawCal(iYear,iMonth,iCellHeight,iDateTextSize){var colorTD=" bgcolor='"+gcMouseOut+"' bordercolor='"+gcMouseOut+"'";var styleTD=" valign='middle' align='center' style='height:"+iCellHeight+"px;font-weight:bolder;font-size:"+iDateTextSize+"px;";var dateCal="";dateCal+="<tr>";for(var i=0;i<7;i++){dateCal+="<td"+colorTD+styleTD+"color:#990099'>"+WeekDay[i]+"</td>";}dateCal+="</tr>";for(var w=1;w<7;w++){dateCal+="<tr>";for(var d=0;d<7;d++){var tmpid=w+""+d;dateCal+="<td"+styleTD+"cursor:pointer;' onclick='fSetSelected("+tmpid+")'>";dateCal+="<span id='cellText"+tmpid+"'></span>";dateCal+="</td>";}dateCal+="</tr>";}return dateCal;} function fUpdateCal(iYear,iMonth){var myMonth=fBuildCal(iYear,iMonth);var i=0;for(var w=1;w<7;w++){for(var d=0;d<7;d++){with($("cellText"+w+""+d)){parentNode.bgColor=gcMouseOut;parentNode.borderColor=gcMouseOut;parentNode.onmouseover=function(){this.bgColor=gcMouseOver;};parentNode.onmouseout=function(){this.bgColor=gcMouseOut;};if(myMonth[w][d]<0){style.color=gcNotCurMonth;innerHTML=Math.abs(myMonth[w][d]);}else{style.color=((d==0)||(d==6))?gcRestDay:gcWorkDay;innerHTML=myMonth[w][d];if(iYear==giYear && iMonth==giMonth && myMonth[w][d]==giDay){style.color=gcToday;parentNode.bgColor=gcTodayMouseOut;parentNode.onmouseover=function(){this.bgColor=gcTodayMouseOver;};parentNode.onmouseout=function(){this.bgColor=gcTodayMouseOut;};}}}}}} function fSetYearMon(iYear,iMon){$("tbSelMonth").options[iMon-1].selected=true;for(var i=0;i<$("tbSelYear").length;i++){if($("tbSelYear").options[i].value==iYear){$("tbSelYear").options[i].selected=true;}}fUpdateCal(iYear,iMon);} function fPrevMonth(){var iMon=$("tbSelMonth").value;var iYear=$("tbSelYear").value;if(--iMon<1){iMon=12;iYear--;}fSetYearMon(iYear,iMon);} function fNextMonth(){var iMon=$("tbSelMonth").value;var iYear=$("tbSelYear").value;if(++iMon>12){iMon=1;iYear++;}fSetYearMon(iYear,iMon);} function fGetXY(aTag){var oTmp=aTag;var pt=new Point(0,0);do{pt.x+=oTmp.offsetLeft;pt.y+=oTmp.offsetTop;oTmp=oTmp.offsetParent;}while(oTmp.tagName.toUpperCase()!="BODY");return pt;} function getDateDiv(){var noSelectForIE="";var noSelectForFireFox="";if(document.all){noSelectForIE="onselectstart='return false;'";}else{noSelectForFireFox="-moz-user-select:none;";}var dateDiv="";dateDiv+="<div id='calendardiv' onclick='event.cancelBubble=true' "+noSelectForIE+" style='"+noSelectForFireFox+"position:absolute;z-index:99;visibility:hidden;border:1px solid #999999;'>";dateDiv+="<table border='0' bgcolor='#E0E0E0' cellpadding='1' cellspacing='1' >";dateDiv+="<tr>";dateDiv+="<td><input type='button' id='PrevMonth' value='<' style='height:20px;width:20px;font-weight:bolder;' onclick='fPrevMonth()'>";dateDiv+="</td><td><select id='tbSelYear' style='border:1px solid;' onchange='fUpdateCal($V(\"tbSelYear\"),$V(\"tbSelMonth\"))'>";for(var i=startYear;i<endYear;i++){dateDiv+="<option value='"+i+"'>"+i+strYear+"</option>";}dateDiv+="</select></td><td>";dateDiv+="<select id='tbSelMonth' style='border:1px solid;' onchange='fUpdateCal($V(\"tbSelYear\"),$V(\"tbSelMonth\"))'>";for(var i=0;i<12;i++){dateDiv+="<option value='"+(i+1)+"'>"+gMonths[i]+"</option>";}dateDiv+="</select></td><td>";dateDiv+="<input type='button' id='NextMonth' value='>' style='height:20px;width:20px;font-weight:bolder;' onclick='fNextMonth()'>";dateDiv+="</td>";dateDiv+="</tr><tr>";dateDiv+="<td align='center' colspan='4'>";dateDiv+="<div style='background-color:#cccccc'><table width='100%' border='0' cellpadding='3' cellspacing='1'>";dateDiv+=fDrawCal(giYear,giMonth,dayTdHeight,dayTdTextSize);dateDiv+="</table></div>";dateDiv+="</td>";dateDiv+="</tr><tr><td align='center' colspan='4' nowrap>";dateDiv+="<span style='cursor:pointer;font-weight:bolder;' onclick='fSetDate(giYear,giMonth,giDay)' onmouseover='this.style.color=\""+gcMouseOver+"\"' onmouseout='this.style.color=\"#000000\"'>"+strToday+":"+giYear+strYear+giMonth+strMonth+giDay+strDay+"</span>";dateDiv+="</tr></tr>";dateDiv+="</table></div>";return dateDiv;} with(document){onclick=fHideCalendar;write(getDateDiv());} </script> </head> <body> <input type="text" style="border:1px solid #999;" onclick="fPopCalendar(event,this,this)" onfocus="this.select()" readonly="readonly" /> </body> </html> 参考资料: http://www.tswzsj.com/html/wzjsxw/287.html

D. 怎样在js里面格式化日期

格式化时间
参数: formatStr 格式化串 y年,m月,d日,h小时,i分钟,s秒钟 缺省值 "y-m-d h:i:s"
fdate 要格式化的时间(时间戳)UTC秒数 缺省值 当前时间
实例: formatDate() 当前时间默认格式 如 2011-4-12 12:51:12
formatDate('y/m/d', 2132132131) 某时间格式为 年月日 如 2010/12/5
function formatDate(formatStr, fdate)
{
var fTime, fStr = 'ymdhis';
if (!formatStr)
formatStr= "y-m-d h:i:s";
if (fdate)
fTime = new Date(fdate);
else
fTime = new Date();
var formatArr = [
fTime.getFullYear().toString(),
(fTime.getMonth()+1).toString(),
fTime.getDate().toString(),
fTime.getHours().toString(),
fTime.getMinutes().toString(),
fTime.getSeconds().toString()
]
for (var i=0; i<formatArr.length; i++)
{
formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
}
return formatStr;
}

E. 在js脚本中怎样修改日期格式,例如2012-12-12修改成2012年12月12日

var nowDate = new Date(); //当前日期
var year = nowDate.getYear(); //获得专年
var Month = nowDate.getMonth(); //获得月
var Date = nowDate.getDate(); //获得日
var outputDate = year+'年'+Month+'月'+Date+'日';

就童鞋你的问题:属
var myDate = new Date('2012-12-12');
var year = myDate .getYear(); //获得年
var Month = myDate .getMonth(); //获得月
var Date = myDate .getDate(); //获得日
var outputDate = year+'年'+Month+'月'+Date+'日';

F. 如何定义js的 日期格式

步骤如下:

一、将日期转换为我们常用的 "Yyyyyymymm-dd hh:mm:ss" 格式, 我们可以获取日期并进行组内装, 如下面的代容码所示:

阅读全文

与js设定日期相关的资料

热点内容
ps入门必备文件 浏览:348
以前的相亲网站怎么没有了 浏览:15
苹果6耳机听歌有滋滋声 浏览:768
怎么彻底删除linux文件 浏览:379
编程中字体的颜色是什么意思 浏览:534
网站关键词多少个字符 浏览:917
汇川am系列用什么编程 浏览:41
笔记本win10我的电脑在哪里打开摄像头 浏览:827
医院单位基本工资去哪个app查询 浏览:18
css源码应该用什么文件 浏览:915
编程ts是什么意思呢 浏览:509
c盘cad占用空间的文件 浏览:89
不锈钢大小头模具如何编程 浏览:972
什么格式的配置文件比较主流 浏览:984
增加目录word 浏览:5
提取不相邻两列数据如何做图表 浏览:45
r9s支持的网络制式 浏览:633
什么是提交事务的编程 浏览:237
win10打字卡住 浏览:774
linux普通用户关机 浏览:114

友情链接