您的位置:

js日历程序(原生js写日历)

本文目录一览:

JS中写日历时候遇见的问题

问题解决不了,我这里有JAVA版的!

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.table.*;

public class MyCalendar extends JApplet {

private static final long serialVersionUID = 4398452463891616204L;

public static final String WEEK_SUN = "SUN";

public static final String WEEK_MON = "MON";

public static final String WEEK_TUE = "TUE";

public static final String WEEK_WED = "WED";

public static final String WEEK_THU = "THU";

public static final String WEEK_FRI = "FRI";

public static final String WEEK_SAT = "SAT";

public static final Color background = Color.white;

public static final Color foreground = Color.black;

public static final Color headerBackground = Color.blue;

public static final Color headerForeground = Color.white;

public static final Color selectedBackground = Color.blue;

public static final Color selectedForeground = Color.white;

private JPanel cPane;

private JLabel yearsLabel;

private JSpinner yearsSpinner;

private JLabel monthsLabel;

private JComboBox monthsComboBox;

private JTable daysTable;

private AbstractTableModel daysModel;

private Calendar calendar;

public MyCalendar() {

cPane = (JPanel) getContentPane();

}

public void init() {

cPane.setLayout(new BorderLayout());

calendar = Calendar.getInstance();

//calendar = Calendar.getInstance();

yearsLabel = new JLabel("Year: ");

yearsSpinner = new JSpinner();

yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));

yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));

yearsSpinner.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent changeEvent) {

int day = calendar.get(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, 1);

calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue()).intValue());

int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day);

updateView();

}

});

JPanel yearMonthPanel = new JPanel();

cPane.add(yearMonthPanel, BorderLayout.NORTH);

yearMonthPanel.setLayout(new BorderLayout());

yearMonthPanel.add(new JPanel(), BorderLayout.CENTER);

JPanel yearPanel = new JPanel();

yearMonthPanel.add(yearPanel, BorderLayout.WEST);

yearPanel.setLayout(new BorderLayout());

yearPanel.add(yearsLabel, BorderLayout.WEST);

yearPanel.add(yearsSpinner, BorderLayout.CENTER);

monthsLabel = new JLabel("Month: ");

monthsComboBox = new JComboBox();

for (int i = 1; i = 12; i++) {

monthsComboBox.addItem(new Integer(i));

}

monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));

monthsComboBox.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent actionEvent) {

int day = calendar.get(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, 1);

calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());

int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day);

updateView();

}

});

JPanel monthPanel = new JPanel();

yearMonthPanel.add(monthPanel, BorderLayout.EAST);

monthPanel.setLayout(new BorderLayout());

monthPanel.add(monthsLabel, BorderLayout.WEST);

monthPanel.add(monthsComboBox, BorderLayout.CENTER);

daysModel = new AbstractTableModel() {

private static final long serialVersionUID = -5996965100536784508L;

public int getRowCount() {

return 7;

}

public int getColumnCount() {

return 7;

}

public Object getValueAt(int row, int column) {

if (row == 0) {

return getHeader(column);

}

row--;

Calendar calendar = (Calendar) MyCalendar.this.calendar.clone();

calendar.set(Calendar.DAY_OF_MONTH, 1);

int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;

int index = row * 7 + column;

int dayIndex = index - moreDayCount + 1;

if (index moreDayCount || dayIndex dayCount) {

return null;

} else {

return new Integer(dayIndex);

}

}

};

daysTable = new CalendarTable(daysModel, calendar);

daysTable.setCellSelectionEnabled(true);

daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer() {

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,

boolean hasFocus, int row, int column) {

String text = (value == null) ? "" : value.toString();

JLabel cell = new JLabel(text);

cell.setOpaque(true);

if (row == 0) {

cell.setForeground(headerForeground);

cell.setBackground(headerBackground);

} else {

if (isSelected) {

cell.setForeground(selectedForeground);

cell.setBackground(selectedBackground);

} else {

cell.setForeground(foreground);

cell.setBackground(background);

}

}

return cell;

}

});

updateView();

cPane.add(daysTable, BorderLayout.CENTER);

}

public static String getHeader(int index) {

switch (index) {

case 0:

return WEEK_SUN;

case 1:

return WEEK_MON;

case 2:

return WEEK_TUE;

case 3:

return WEEK_WED;

case 4:

return WEEK_THU;

case 5:

return WEEK_FRI;

case 6:

return WEEK_SAT;

default:

return null;

}

}

public void updateView() {

daysModel.fireTableDataChanged();

daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),

calendar.get(Calendar.WEEK_OF_MONTH));

daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK) - 1,

calendar.get(Calendar.DAY_OF_WEEK) - 1);

}

public static class CalendarTable extends JTable {

private static final long serialVersionUID = 2808554667609229840L;

private Calendar calendar;

public CalendarTable(TableModel model, Calendar calendar) {

super(model);

this.calendar = calendar;

}

public void changeSelection(int row, int column, boolean toggle, boolean extend) {

super.changeSelection(row, column, toggle, extend);

if (row == 0) {

return;

}

Object obj = getValueAt(row, column);

if (obj != null) {

calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());

}

}

}

@SuppressWarnings("deprecation")

public static void main(String[] args) {

JFrame frame = new JFrame("Calendar Application");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyCalendar myCalendar = new MyCalendar();

myCalendar.init();

frame.getContentPane().add(myCalendar);

frame.setSize(240, 172);

frame.show();

}

}

PC端如何用js做点击日历选中一周

1.写一个获取当前时间的脚本

2.写一个获取事件源绝对坐标的脚本

3.写一个日历生成脚本,根据年(闰年)月(大小月)周(周一到周日)生成日历

4.当事件源(一般是文本框)获得焦点的时候,触发获取绝对坐标的事件,然后生成当前月的日历,将此日历的坐标用脚本移动到文本框下方

5.生成日历的时候,给所有日添加返回事件,把当前选中的年月日反馈

js编写一个显示程序运行当天到下一个元旦之间的天数。

下面小编就为大家带来一篇js编写当天简单日历效果【实现代码】。小编觉得挺不错的,现在分享给大家,也给大家做个参考

之前一直很想用javascript写一个日历,但是因为完全没有好的思路, 所以迟迟没有尝试。最近在网上刚好看到用javascript编写的简单日历的例子,代码量虽然不大, 但是我觉得很好地阐述了js日历的实现原理。自己也尝试着做了一下,收获蛮大,掌握了基本的实现原理后,再想增加更多的功能,完全就可以自由发挥了,先在这里分享一下吧,有兴趣的可以试试!

一.表格行数问题

既然要显示日期表格的话,首先得知道这个表格有多少行多少列,列数是已经确定的,从星期天(日历上第1列是星期天)到星期六一共7列。要解决行数问题之前,还得先知道这个月的第1天是星期几,因为每个月的1号并不都是从日历上的星期天开始排的,可能1号是星期五,星期六也说不定,所以1号的左边部分,就得用空表格代替了。那么用多少个空表格代替呢,这里就得用到getDay()方法了,该方法返回数组[0-6]中的一个数字,0代表星期天,1代表星期一,2代表星期二,以此类推。所以如果一个月的1号是星期五的话,那么刚好左边需要5个空表格代替。然后,假如一个月有31天,最后求出的表格行数就是:

var tr_nums = Math.ceil((5 + 31)/7); 

当然,并不是每个月都是31天,所以我们得创建一个包含12个月份的数组,每个元素代表每个月份所包含的天数。但是2月份比较特殊,闰年的2月份有29天,平年的2月份只有28天。所以,在创建数组之前,得自己创建一个判断闰年的函数:

//如果当前年份能被4整除但是不能被100整除或者能被400整除,即可确定为闰年,返回1,否则返回0

function isLeap(year) {

 return year % 4 == 0 ? (year % 100 != 0 ? 1 : (year % 400 == 0 ? 1 : 0)) : 0;

日历用js如何实现

script src="js/Calendar.js" type="text/javascript" language="javascript"/script

加入到你的页面中 js/Calendar.js 这是控件的路径

input name="txtDate" type="text" onclick="SelectDate(this)" readonly="readonly"

Calendar.js 这个东西应该可以找的到吧,onclick="SelectDate(this)" 这是调用的方法

关于用JAVASCRIPT做日历的问题!

html

headmeta http-equiv="Content-Type" content="text/html; charset=gb2312"

title日期选择 - BY ziyue/title

script language="JavaScript" type="text/JavaScript"

/*=======Calendar.js=======By Jiang Hongbin=======*/

var months = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");

var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

var days = new Array("日","一", "二", "三", "四", "五", "六");

var today;

document.writeln("div id='Calendar' style='position:absolute; z-index:1; visibility: hidden; filter:\"progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#999999,strength=3)\"'/div");

function getDays(month, year)

{

//下面的这段代码是判断当前是否是闰年的

if (1 == month)

return ((0 == year % 4) (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;

else

return daysInMonth[month];

}

function getToday()

{

//得到今天的年,月,日

this.now = new Date();

this.year = this.now.getFullYear();

this.month = this.now.getMonth();

this.day = this.now.getDate();

}

function getStringDay(str)

{

//得到输入框的年,月,日

var str=str.split("-")

this.now = new Date(parseFloat(str[0]),parseFloat(str[1])-1,parseFloat(str[2]));

this.year = this.now.getFullYear();

this.month = this.now.getMonth();

this.day = this.now.getDate();

}

function newCalendar() {

var parseYear = parseInt(document.all.Year.options[document.all.Year.selectedIndex].value);

var newCal = new Date(parseYear, document.all.Month.selectedIndex, 1);

var day = -1;

var startDay = newCal.getDay();

var daily = 0;

if ((today.year == newCal.getFullYear()) (today.month == newCal.getMonth()))

day = today.day;

var tableCal = document.all.calendar;

var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());

for (var intWeek = 1; intWeek tableCal.rows.length;intWeek++)

for (var intDay = 0;intDay tableCal.rows[intWeek].cells.length;intDay++)

{

var cell = tableCal.rows[intWeek].cells[intDay];

if ((intDay == startDay) (0 == daily))

daily = 1;

if(day==daily) //今天,调用今天的Class

{

cell.style.background='#6699CC';

cell.style.color='#FFFFFF';

//cell.style.fontWeight='bold';

}

else if(intDay==6) //周六

cell.style.color='green';

else if (intDay==0) //周日

cell.style.color='red';

if ((daily 0) (daily = intDaysInMonth))

{

cell.innerText = daily;

daily++;

}

else

cell.innerText = "";

}

}

function GetDate(InputBox)

{

var sDate;

//这段代码处理鼠标点击的情况

if (event.srcElement.tagName == "TD")

if (event.srcElement.innerText != "")

{

sDate = document.all.Year.value + "-" + document.all.Month.value + "-" + event.srcElement.innerText;

eval("document.all."+InputBox).value=sDate;

HiddenCalendar();

}

}

function HiddenCalendar()

{

//关闭选择窗口

document.all.Calendar.style.visibility='hidden';

}

function ShowCalendar(InputBox)

{

var x,y,intLoop,intWeeks,intDays;

var DivContent;

var year,month,day;

//var o=document.getElementById(InputBox);

var o=InputBox;

var oid=o.id;

var thisyear; //真正的今年年份

if(!oid)oid=o.name;

thisyear=new getToday();

thisyear=thisyear.year;

today = o.value;

if(isDate(today))

today = new getStringDay(today);

else

today = new getToday();

//显示的位置

x=o.offsetLeft;

y=o.offsetTop;

while(o=o.offsetParent)

{

x+=o.offsetLeft;

y+=o.offsetTop;

}

document.all.Calendar.style.left=x+2;

document.all.Calendar.style.top=y+20;

document.all.Calendar.style.visibility="visible";

//下面开始输出日历表格(border-color:#9DBAF7)

DivContent="table border='0' cellspacing='0' style='border:1px solid #0066FF; background-color:#EDF2FC'";

DivContent+="tr";

DivContent+="td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA'";

//年

DivContent+="select name='Year' id='Year' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'";

for (intLoop = thisyear - 100; intLoop (thisyear + 2); intLoop++)

DivContent+="option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + "" + intLoop + "/option";

DivContent+="/select";

//月

DivContent+="select name='Month' id='Month' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'";

for (intLoop = 0; intLoop months.length; intLoop++)

DivContent+="option value= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + "" + months[intLoop] + "/option";

DivContent+="/select";

DivContent+="/td";

DivContent+="td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA; font-weight:bold; font-family:Wingdings 2,Wingdings,Webdings; font-size:16px; padding-top:2px; color:#4477FF; cursor:hand' align='center' title='关闭' onClick='javascript:HiddenCalendar()'S/td";

DivContent+="/tr";

DivContent+="trtd align='center' colspan='2'";

DivContent+="table id='calendar' border='0' width='100%'";

//星期

DivContent+="tr";

for (intLoop = 0; intLoop days.length; intLoop++)

DivContent+="td align='center' style='font-size:12px'" + days[intLoop] + "/td";

DivContent+="/tr";

//天

for (intWeeks = 0; intWeeks 6; intWeeks++)

{

DivContent+="tr";

for (intDays = 0; intDays days.length; intDays++)

DivContent+="td onClick='GetDate(\"" + oid + "\")' style='cursor:hand; border-right:1px solid #BBBBBB; border-bottom:1px solid #BBBBBB; color:#215DC6; font-family:Verdana; font-size:12px' align='center'/td";

DivContent+="/tr";

}

DivContent+="/table/td/tr/table";

document.all.Calendar.innerHTML=DivContent;

newCalendar();

}

function isDate(dateStr)

{

var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;

var matchArray = dateStr.match(datePat);

if (matchArray == null) return false;

var month = matchArray[3];

var day = matchArray[5];

var year = matchArray[1];

if (month 1 || month 12) return false;

if (day 1 || day 31) return false;

if ((month==4 || month==6 || month==9 || month==11) day==31) return false;

if (month == 2)

{

var isleap = (year % 4 == 0 (year % 100 != 0 || year % 400 == 0));

if (day 29 || (day==29 !isleap)) return false;

}

return true;

}

/script

style type="text/css"

!--

td,input {

font-family: Verdana, Arial, Helvetica, sans-serif;

font-size: 12px;

}

--

/style

/head

body

form name="form1" method="post" action=""

table width="500" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC"

tr bgcolor="#FFFFFF"

td align="right"您的生日:/td

tdinput name="birthday" type="text" id="birthday" title="点击选择" onClick="javascript:ShowCalendar(this)" size="20"

input type="button" name="Submit" value="选 择" onClick="javascript:ShowCalendar(form1.birthday)"/td

tdBLOG:a href="";/a/td

/tr

/table

/form

/body

/html

js日历程序(原生js写日历)

2022-11-16
php或js日历,php做日历

2022-11-22
php日历控件,php写日历

2022-11-30
简易js网页实时时钟日历,js电子时钟代码

本文目录一览: 1、如何使用JS实现一个简易数码时钟 2、求一款js做的时间日历控件? 3、怎样在jsp页面中显示日历(高手帮下忙啦!很急) 4、用js在网页上显示当前日期和时间,并显示是星期几 5、

2023-12-08
php日历控件(PHP制作日历)

2022-11-10
js高级程序设计笔记14(js高级程序设计笔记14页)

本文目录一览: 1、JavaScript高级程序设计 该怎么看 2、JavaScript学习笔记之数组基本操作示例 3、JS中有关sort以及return的问题 JavaScript高级程序设计 该怎

2023-12-08
php日历插件,php制作漂亮日历

2022-11-23
小程序日历插件详解

2023-05-18
淘宝网日历控件js代码,web日历控件

2022-11-23
php写控件的日历(PHP制作日历)

2022-11-12
php日历按钮(php日历和时间)

2022-11-16
小程序js遍历,前端js遍历

2022-11-24
htmljs编程笔记(html代码笔记)

本文目录一览: 1、html代码和JS代码有什么区别 2、如何在html中调用js函数 3、JavaScript学习笔记之数组基本操作示例 4、HTML5初学者笔记 5、《web前端笔记7》js字符—

2023-12-08
aspnetjs日历控件(aspnet日历控件选择日期)

本文目录一览: 1、ASP.NET:将TextBox绑定日历Calendar控件,运行时,默认显示当天日期 2、asp.net关于调用calendar的问题 3、asp中怎样使用日历控件 4、求好用的

2023-12-08
原生js写的项目怎么做语言处理,原生js编程

2022-11-26
js记录信息代码(js历史记录)

本文目录一览: 1、js如何实现历史记录? 2、js代码怎么写 3、百度统计 事件跟踪 js代码怎么写 4、如何用js实现调用手机摄像头扫描条形码然后带出条形码的值到文本框 5、js实现cookie历

2023-12-08
小程序日历全攻略

2023-05-21
超牛js版万年历源码,js制作万年历

本文目录一览: 1、万年历查询程序. C语言代码 2、JS万年历中如何求某天的节气 3、如何用JavaScript编写一个万年历 4、求论坛日历JS代码 万年历查询程序. C语言代码 除了天数差计算,

2023-12-08
js日历考勤表代码,日历考勤表怎么弄的

本文目录一览: 1、考勤表上面代码代表什么意思 2、sql中使用两个join后出现了每行记录重复大约九次的情况 3、我想在EXCEL中以日历形式做一个考勤表,怎么做呢? 4、考勤表时间没有打上指纹在异

2023-12-08
农历时间js代码下载(js获取农历时间)

本文目录一览: 1、求一段JAVASCRIPT的时间显示代码, 2、农历转阳历JS代码 3、高手帮解决一个JS显示农历时间代码错误问题,真的很谢谢! 4、求js详细时间代码,年月日星期时分(秒),阴历

2023-12-08