I am including Javascript code that is used to pop up a calendar. The problem with the code is that I want a '0' to be inserted in front of the month or day if the month lies between 1--9 and days between 1--9 in the calendar. The following is the script. Can anybody guide me to the modifications?
我包含用于弹出日历的Javascript代码。代码的问题在于,如果月份介于日历中的1-9到1--9之间,那么我希望在月份或日期之前插入“0”。以下是脚本。任何人都可以指导我进行修改吗?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
<!--
var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var days = new Array("S", "M", "T", "W", "T", "F", "S");
today = new getToday();
var element_id;
function getDays(month, year)
{
// Test for leap year when February is selected.
if (1 == month)
return ((0 == year % 4) && (0 != (year % 100))) ||
(0 == year % 400) ? 29 : 28;
else
return daysInMonth[month];
}
function getToday()
{
// Generate today's date.
this.now = new Date();
this.year = this.now.getFullYear() ; // Returned year XXXX
this.month = this.now.getMonth();
this.day = this.now.getDate();
}
function newCalendar()
{
var parseYear = parseInt(document.all.year [document.all.year.selectedIndex].text);
var newCal = new Date(parseYear , document.all.month.selectedIndex, 1);
var day = -1;
var startDay = newCal.getDay();
var daily = 0;
today = new getToday(); // 1st call
if ((today.year == newCal.getFullYear() ) && (today.month == newCal.getMonth()))
day = today.day;
// Cache the calendar table's tBody section, dayList.
var tableCal = document.all.calendar.tBodies.dayList;
var intDaysInMonth =
getDays(newCal.getMonth(), newCal.getFullYear() );
for (var intWeek = 0; intWeek < tableCal.rows.length; intWeek++)
for (var intDay = 0;
intDay < tableCal.rows[intWeek].cells.length;
intDay++)
{
var cell = tableCal.rows[intWeek].cells[intDay];
// Start counting days.
if ((intDay == startDay) && (0 == daily))
daily = 1;
// Highlight the current day.
cell.style.color = (day == daily) ? "red" : "";
if(day == daily)
{
document.all.todayday.innerText= "Today: " + day + "/" +
(newCal.getMonth()+1) + "/" + newCal.getFullYear() ;
}
// Output the day number into the cell.
if ((daily > 0) && (daily <= intDaysInMonth))
cell.innerText = daily++;
else
cell.innerText = "";
}
}
function getTodayDay()
{
document.all[element_id].value = today.year + "/" + (today.month+1) +
"/" + today.day;
//document.all.calendar.style.visibility="hidden";
document.all.calendar.style.display="none";
document.all.year.selectedIndex =100;
document.all.month.selectedIndex = today.month;
}
function getDate()
{
// This code executes when the user clicks on a day
// in the calendar.
if ("TD" == event.srcElement.tagName)
// Test whether day is valid.
if ("" != event.srcElement.innerText)
{
var mn = document.all.month.selectedIndex+1;
var Year = document.all.year [document.all.year.selectedIndex].text;
document.all[element_id].value=Year +"-"+mn+"-"+event.srcElement.innerText;
document.all.calendar.style.display="none";
}
}
function GetBodyOffsetX(el_name, shift)
{
var x;
var y;
x = 0;
y = 0;
var elem = document.all[el_name];
do
{
x += elem.offsetLeft;
y += elem.offsetTop;
if (elem.tagName == "BODY")
break;
elem = elem.offsetParent;
} while (1 > 0);
shift[0] = x;
shift[1] = y;
return x;
}
function SetCalendarOnElement(el_name)
{
if (el_name=="")
el_name = element_id;
var shift = new Array(2);
GetBodyOffsetX(el_name, shift);
document.all.calendar.style.pixelLeft = shift[0]; // - document.all.calendar.offsetLeft;
document.all.calendar.style.pixelTop = shift[1] + 25 ;
}
function ShowCalendar(elem_name)
{
if (elem_name=="")
elem_name = element_id;
element_id = elem_name; // element_id is global variable
newCalendar();
SetCalendarOnElement(element_id);
//document.all.calendar.style.visibility = "visible";
document.all.calendar.style.display="inline";
}
function HideCalendar()
{
//document.all.calendar.style.visibility="hidden";
document.all.calendar.style.display="none";
}
function toggleCalendar(elem_name)
{
//if (document.all.calendar.style.visibility == "hidden")
if(document.all.calendar.style.display=="none")
ShowCalendar(elem_name);
else
HideCalendar();
}
-->
</script>
<style>
.today {COLOR: black; FONT-FAMILY: sans-serif; FONT-SIZE: 10pt; FONT-WEIGHT: bold}
.days {COLOR: navy; FONT-FAMILY: sans-serif; FONT-SIZE: 10pt; FONT-WEIGHT: bold; TEXT-ALIGN: center}
.dates {COLOR: black; FONT-FAMILY: sans-serif; FONT-SIZE: 10pt}
</style>
</head>
<body bgcolor="#FFF8DC">
<h5>Diesel Generators</h5><br><br>
<br>
<br>
<script language="JavaScript">
function toggle(source) {
checkboxes = document.getElementsByName('DG1');
for each(var checkbox in checkboxes)
checkbox.checked = source.checked;
checkboxes = document.getElementsByName('DG2');
for each(var checkbox in checkboxes)
checkbox.checked = source.checked;
checkboxes = document.getElementsByName('DG3');
for each(var checkbox in checkboxes)
checkbox.checked = source.checked;
checkboxes = document.getElementsByName('DG4');
for each(var checkbox in checkboxes)
checkbox.checked = source.checked;
checkboxes = document.getElementsByName('DG5');
for each(var checkbox in checkboxes)
checkbox.checked = source.checked;
}
</script>
<input type="checkbox" onClick="toggle(this)" /> Select All<br/>
<form method=POST action="DGUsageServlet">
<INPUT TYPE="CHECKBOX" name="DG1">DG1
<INPUT TYPE="CHECKBOX" name="DG2">DG2
<INPUT TYPE="CHECKBOX" name="DG3">DG3
<INPUT TYPE="CHECKBOX" name="DG4">DG4
<INPUT TYPE="CHECKBOX" name="DG5">DG5
<br>
<br>
<INPUT TYPE=TEXT NAME="StartDate">
<a href="JavaScript:;" onClick="toggleCalendar('StartDate')">StartDate</a>
<INPUT TYPE=TEXT NAME="EndDate">
<a href="JavaScript:;" onClick="toggleCalendar('EndDate')">EndDate</a>
<input name=plotgraph value=plotgraph align="CENTER" type=submit>
</form>
<TABLE bgColor=#ffffff border=1 cellPadding=0 cellSpacing=3 id=calendar style="DISPLAY: none; POSITION: absolute; Z-INDEX: 4">
<TBODY>
<TR>
<TD colSpan=7 vAlign=center>
<!-- Month combo box -->
<SELECT id=month onchange=newCalendar()>
<script type="text/javascript">
// Output months into the document.
// Select current month.
for (var intLoop = 0; intLoop < months.length; intLoop++)
document.write("<OPTION " + (today.month == intLoop ? "Selected" : "") + ">" + months[intLoop]);
</SCRIPT>
</SELECT>
<!-- Year combo box -->
<SELECT id=year onchange=newCalendar()>
<script type="text/javascript">
// Output years into the document.
// Select current year.
for (var intLoop = 1900; intLoop < 2028; intLoop++)
document.write("<OPTION " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop);
</SCRIPT>
</SELECT>
</TD>
</TR>
<TR class=days>
<!-- Generate column for each day. -->
<script type="text/javascript">
// Output days.
for (var intLoop = 0; intLoop < days.length; intLoop++)
document.write("<TD>" + days[intLoop] + "</TD>");
</SCRIPT>
</TR>
<TBODY class=dates id=dayList onclick="getDate('')" vAlign=center>
<!-- Generate grid for individual days. -->
<script type="text/javascript">
for (var intWeeks = 0; intWeeks < 6; intWeeks++)
{
document.write("<TR>");
for (var intDays = 0; intDays < days.length; intDays++)
document.write("<TD>" +intDays+ "</TD>");
document.write("</TR>");
}
</SCRIPT>
<!-- Generate today day. --></TBODY>
<TBODY>
<TR>
<TD class=today colSpan=5 id=todayday onclick=getTodayDay()></TD>
<TD align=right colSpan=2><A href="javascript:HideCalendar();"><SPAN style="COLOR: black; FONT-SIZE: 10px"><B>Hide</B></SPAN></A></TD>
</TR>
</TBODY>
</TABLE>
</body>
</html>
3 个解决方案
#1
I don't think the way you are writing the script that it is ever going to work as intended. Try looking on google for a pre-made solution. There is no shame in that.
我不认为你编写脚本的方式是按照预期的方式工作。尝试在谷歌上寻找预先制定的解决方案。没有羞耻感。
https://engineering.purdue.edu/ECN/Support/KB/Docs/JavascriptCalendar
#2
I agree with Nelson,
我同意尼尔森的观点,
This is one of the best javascript date picker solutions.
这是最好的javascript日期选择器解决方案之一。
#3
Just add a zero string to the front of it, and return two last characters using .substring(-2,2), like so:
只需在其前面添加一个零字符串,然后使用.substring(-2,2)返回最后两个字符,如下所示:
var iv="3";
alert(("0"+iv).substring(-2,2));
iv=1;
alert(("0"+iv).substring(-2,2));
iv=2;
alert(("0"+iv).substring(-2,2));
It does not matter if the input is a string or digit.
输入是字符串还是数字无关紧要。
#1
I don't think the way you are writing the script that it is ever going to work as intended. Try looking on google for a pre-made solution. There is no shame in that.
我不认为你编写脚本的方式是按照预期的方式工作。尝试在谷歌上寻找预先制定的解决方案。没有羞耻感。
https://engineering.purdue.edu/ECN/Support/KB/Docs/JavascriptCalendar
#2
I agree with Nelson,
我同意尼尔森的观点,
This is one of the best javascript date picker solutions.
这是最好的javascript日期选择器解决方案之一。
#3
Just add a zero string to the front of it, and return two last characters using .substring(-2,2), like so:
只需在其前面添加一个零字符串,然后使用.substring(-2,2)返回最后两个字符,如下所示:
var iv="3";
alert(("0"+iv).substring(-2,2));
iv=1;
alert(("0"+iv).substring(-2,2));
iv=2;
alert(("0"+iv).substring(-2,2));
It does not matter if the input is a string or digit.
输入是字符串还是数字无关紧要。