想把日期显示成 yyyy-mm-dd格式的,搞不定了。谢谢!

lixiaz 2004-01-08 03:58:06
/************************************

This is a DateCom,you can use it in you web program. you can use the DateCom step by step following
(1) Add the function showDateCom in where you want,the showDateCom defined with showDateCom(obj)
*************************************/

var theDate = new Date()
var theYear = theDate.getFullYear()
var theMonth = theDate.getMonth()
var theDay = theDate.getDate()
var CalendarValue=theYear+"."+(theMonth+1)+"."+theDate
var theObj

function leapYear(year) //判断是否是润年
{
if(year % 4 ==0)
return true
else
return false
}

function getDays(month,year)
{
var ar=new Array(31,29,31,30,31,30,31,31,30,31,30,31)
ar[1]=(leapYear(year))?29:28
return ar[month]
}

function display_Calendar() {

//get Year and Month from form
var CurYear=DateForm.CurrYear.value
var CurMonth=DateForm.CurrMonth.value

//create current date object
var CurrDate = new Date(CurYear,CurMonth,theDay)

var replaceText = ''
replaceText += ' <table border="0" cellspacing="0" cellpadding="0" width="210" bgcolor="#FFFFFF" style="font-size:12px">\n'
replaceText += ' <tr height="20" bgcolor="#008080">\n'
replaceText += ' <td> 日</td>\n'
replaceText += ' <td> 一</td>\n'
replaceText += ' <td> 二</td>\n'
replaceText += ' <td> 三</td>\n'
replaceText += ' <td> 四</td>\n'
replaceText += ' <td> 五</td>\n'
replaceText += ' <td> 六</td>\n'
replaceText += ' </tr>\n'

//get the first day of month is what day is?
var firstDayInstance= new Date(CurYear,CurMonth,1)
var firstDay=firstDayInstance.getDay()

//get the days of CurMonth of CurYear
var theDays = getDays(CurMonth,CurYear)

//the first row of calendar
var i=0; //Record days of the first row fo calendar
replaceText += ' <tr bgcolor="#FFFFFF" height="20">\n'
//the empty cells of first row
for(var j=0;j<firstDay;j++)
replaceText += ' <td> </td>\n'
//the leaves days of first row
for(var j=firstDay;j<7;j++)
{
i++
if(i==theDay)
replaceText += ' <td> <font color="#FF1111"><b>'+i+'</b></font></td>\n'
else
replaceText += ' <td> <a style="cursor:hand" onclick="getCurrDate(\''+DateForm.CurrYear.value+'-'+(Math.abs(DateForm.CurrMonth.value)+1)+'-'+i+'\')">'+i+'</a></td>\n'
}
replaceText += ' </tr>\n'

//out print others days
var row=0
for( ;row<Math.ceil((theDays-i)/7);row++)
{
replaceText += ' <tr bgcolor="#FFFFFF" height="20">\n'
for(var j=1;j<=7;j++)
if((row*7+i+j)<=theDays)
if(row*7+j+i==theDay)
replaceText += ' <td> <font color="#FF1111"><b><a style="cursor:hand" onclick="getCurrDate(\''+DateForm.CurrYear.value+'-'+(Math.abs(DateForm.CurrMonth.value)+1)+'-'+(row*7+j+i)+'\')">'+(row*7+j+i)+'</a></b></font></td>\n'
else
replaceText += ' <td> <a style="cursor:hand" onclick="getCurrDate(\''+DateForm.CurrYear.value+'-'+(Math.abs(DateForm.CurrMonth.value)+1)+'-'+(row*7+j+i)+'\')">'+(row*7+j+i)+'</a></td>\n'
else
replaceText += ' <td> </td>\n'
replaceText += ' </tr>\n'
}

//if there is not row 5 then print row 5 with empty
if(row<5)
{
replaceText += ' <tr bgcolor="#FFFFFF" height="20">\n'
for(var j=1;j<=7;j++)
replaceText += ' <td> </td>\n'
replaceText += ' </tr>\n'
}

replaceText += ' </table>\n'

// replace the current content with the new prepared content
if ((document.layers)? true:false)
{
var lyr = document.layers["newDate"].document;
lyr.open();
lyr.writeln(replaceText);
lyr.close();
}
else
{
document.all["newDate"].innerHTML = replaceText;
}
}

//change Year when click button
function changeYear(isUp)
{
if(isUp==1)
DateForm.CurrYear.value=Math.abs(DateForm.CurrYear.value)+1
else
DateForm.CurrYear.value=Math.abs(DateForm.CurrYear.value)-1
display_Calendar()
}

//get the CalendarValue :getDateComValue(obj,DateStr)
function getCurrDate(DateStr)
{
theObj.value=DateStr
if ((document.layers)? true:false)
document.layers.DivDateCom.visibility="hide"
else
document.all.DivDateCom.style.visibility="hidden"
}

function createDateCom()
{
document.writeln('<div id="DivDateCom" name="DivDateCom" style="position:absolute;TOP:0px;LEFT:0px;width:210;height:120;Z-INDEX:2;visibility:hidden">')
document.writeln('<table border="0" cellspacing="1" cellpadding="1" width="210" bgcolor="#008080">')
document.writeln(' <tr>')
document.writeln(' <td>')
document.writeln(' <table border="0" cellpadding="0" cellspacing="0" width="210" bgcolor="#c3c3c3">')
document.writeln(' <tr>')
document.writeln(' <form name="DateForm">')
document.writeln(' <td>')
document.writeln(' <table border="0" cellpadding="0" cellspacing="0">')
document.writeln(' <tr>')
document.writeln(' <td><input style="height: 20px; font-size: 12px" type="text" name="CurrYear" size="10" maxlength=4 readonly value=""></td>')
document.writeln(' <td><a style="cursor:hand" onclick="changeYear(1)"><img border="0" src="images/up.gif" width="13" height="10"></a><br>')
document.writeln(' <a style="cursor:hand" onclick="changeYear(0)"><img border="0" src="images/down.gif" width="13" height="10"></a></td>')
document.writeln(' </tr>')
document.writeln(' </table>')
document.writeln(' </td>')
document.writeln(' <td align="right"><br>')
document.writeln(' </td>')
document.writeln(' <td align="right">')

document.writeln(' <select size="1" name="CurrMonth" style="height: 20px; font-size: 12px" onchange="display_Calendar()">')
var ar=new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月")
for (var i=0;i<12;i++)
document.writeln(' <option value='+i+'>'+ar[i]+'</option>')
document.writeln(' </select>')

DateForm.CurrYear.value=theYear
DateForm.CurrMonth.value=theMonth

document.writeln(' </td>')
document.writeln(' </form>')
document.writeln(' </tr>')
document.writeln(' </table>')
document.writeln(' <div name="newDate" id="newDate">')

display_Calendar()

document.writeln(' </div>')
document.writeln(' </td>')
document.writeln(' </tr>')
document.writeln('</table>')
document.writeln('</div>')
}

function initialDateCom()
{
DateForm.CurrYear.value=theYear
DateForm.CurrMonth.value=theMonth
display_Calendar()
}

function showDateCom(obj)
{
theObj=obj
initialDateCom()
doMouseMove()
if ((document.layers)? true:false)
document.layers.DivDateCom.visibility="show"
else
document.all.DivDateCom.style.visibility="visible"
}

function doMouseMove() {
/*
if (event.clientX +20 + 210< document.body.clientWidth)
{
newleft=event.clientX+10
}
else
{
if(event.clientX>210+20)
newleft=event.clientX-10
else
newleft=event.clientX-110
}
*/
document.all.DivDateCom.style.left=(document.body.clientWidth-210)/2 //newleft
/*
if (event.clientY > 20+120)
{
newtop=event.clientY-120
}
else
{
if(event.clientY+120+20<document.body.clientHeight)
newtop=event.clientY+10
else
newtop=event.clientY-60
//newtop=document.body.scrollTop + event.clientY
}
*/
document.all.DivDateCom.style.top=(document.body.clientHeight-120)/2//event.clientY //newtop

}

createDateCom()
...全文
134 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zswangII 2004-01-08
  • 打赏
  • 举报
回复
//try
function OutPutDate()
{
var today=new Date()
var year=today.getYear();
var month=today.getMonth()+1;
var date=today.getDate();
var monthspace=month<10?"0":"";
var datespace=date<10?"0":"";
var strFormatDate="";
strFormatDate=year+"-"+monthspace+month+"-"+datespace+date;
return strFormatDate;
}
lixiaz 2004-01-08
  • 打赏
  • 举报
回复
用上面的方法
今天日期是2004-1-8
而我要的是2004-01-08
ptyt 2004-01-08
  • 打赏
  • 举报
回复
有许多,时间的js ,下载一个就好了。
gjd111686 2004-01-08
  • 打赏
  • 举报
回复
function OutPutDate()
{
var today=new Date()
var year=today.getYear();
var month=today.getMonth()+1;
var date=today.getDate();

var strFormatDate="";
strFormatDate=year+"-"+month+"-"+date;
return strFormatDate;
}

87,903

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧