C#输出日历
用C#输出日历,此功能可用于Ajax方式列出计划日程相关的内容,由于是C#控制输出,可以方便加上自己需要的业务处理逻辑。
1.控制台输出:
- using System;
- namespace 控制台日历
- {
- class Program
- {
- public static void Main(string[] args)
- {
- string s = " ";
- Console.WriteLine("输入年份:");
- int nYear = int.Parse(Console.ReadLine());
- Console.WriteLine("输入月份:");
- int nMonth = int.Parse(Console.ReadLine());
- DateTime day1 = new DateTime(nYear,nMonth,1);
- Console.WriteLine("{0}/{1}",day1.Year,day1.Month);
- Console.WriteLine("日 一 二 三 四 五 六");
- int week1 =(int )day1.DayOfWeek;//获取当年当月1号的星期
- //Console.WriteLine("当月一号的星期{0}",week1);
- int lastday = day1.AddMonths(1).AddDays(-1).Day; //获取当月的最后一天
- for (int i = 0; i < week1; i++)
- Console.Write(s);//不能换行输出
- for (int i = 1; i <= lastday; i++)
- {
- Console.Write("{0:00} ", i);//按01 02 输出
- if ((i + week1) % 7 == 0)
- Console.WriteLine();
- }
- Console.WriteLine();
- Console.Write("Press any key to continue . . . ");
- Console.ReadKey(true);
- }
- }
- }
效果图:

2.Html表格输出:
- #region 生成表格日历
- /// <summary>
- /// 生成表格日历 index:月份偏量,用来查看上一月下一月
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- public static string GetCalendarHtml(int index = 0)
- {
- DateTime day1 = new DateTime(DateTime.Now.AddMonths(index).Year, DateTime.Now.AddMonths(index).Month, 1);
- int week1 = (int)day1.DayOfWeek;//获取当年当月1号的星期
- int lastday = day1.AddMonths(1).AddDays(-1).Day; //获取当月的最后一天
- System.Text.StringBuilder builder = new System.Text.StringBuilder();
- builder.Append(string.Format("<table class='calendar_table'><caption><span style='cursor:pointer' class='prevMonth' onclick='javascript:changeMonth(-1)'>上一月</span><span class='currMonth'> {0}年{1}月</span><span style='cursor:pointer' class='nextMonth' onclick='javascript:changeMonth(1)'>下一月</span></caption>", DateTime.Now.AddMonths(index).Year, DateTime.Now.AddMonths(index).Month));
- builder.Append("<tr class='calendar_head'>");
- builder.Append("<td class='calendar_cell'>日</td>");
- builder.Append("<td class='calendar_cell'>一</td>");
- builder.Append("<td class='calendar_cell'>二</td>");
- builder.Append("<td class='calendar_cell'>三</td>");
- builder.Append("<td class='calendar_cell'>四</td>");
- builder.Append("<td class='calendar_cell'>五</td>");
- builder.Append("<td class='calendar_cell'>六</td>");
- builder.Append("</tr>");
- string emptyString = "<td class='calendar_cell'> </td>";
- if (week1 > 0)
- {
- builder.Append("<tr class='calendar_body'>");
- for (int i = 0; i < week1; i++)
- {
- builder.Append(emptyString);
- }
- }
- for (int i = 1; i <= lastday; i++)
- {
- string day = string.Format("{0:00} ", i);//按01 02 输出
- builder.Append(string.Format("<td class='calendar_cell'>{0}</td>", day));
- if ((i + week1) % 7 == 0)
- {
- builder.Append("</tr><tr class='calendar_body'>");
- }
- }
- builder.Append("</tr>");
- builder.Append("</table>");
- return builder.ToString();
- }
- #endregion
页面前台结合javascript实现ajax日历切换效果,只需用js改变函数中的index偏移量即可。
C#输出日历的更多相关文章
- JS输出日历
页面HTML代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...
- PHP程序输出日历
以下代码只是简单实现日历的效果和逻辑思路,没有使用类封装,权当抛砖引玉,有兴趣的朋友可以封装起来,方便调用. <?php /** * PHP利用时间函数输出日历 * Rain.zen $ int ...
- C语言 · 输出日历
算法提高 输出日历 时间限制:1.0s 内存限制:512.0MB 按照下述格式打印2006年12月日历: Calendar 2006-12---------------------- ...
- Java输出日历写法
package TestString_2; import java.text.ParseException;import java.util.Calendar;import java.util.Gre ...
- Java输出日历
源码链接:http://pan.baidu.com/s/1o6xeybK
- Python学习实践-----打印日历
使用python语言实现在控制台打印日历 输入年.月.日 输出对应日历,指定的日数输出为'--' 程序没有做严格的输入验证,故输入整数即可. 以下为没有优化的源码: print_calendar.py ...
- javascript js写特效日历
<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 创建一个js日历(原生JS实现日历)
前言 方法是有参考网上一些资料的,比如闰年的判断,比如每个月第一天是星期几的判断.不说太多,拆分出一个个函数,希望能描述尽可能的清晰明了. 一,判断闰年 //判断闰年 function runNian ...
- .net mvc页面UI之Jquery博客日历控件
摘要:最近在做一个博客系统,其他需要用到博客日历控件,网上搜索了很多资料,其中大部分都是javascript的,经过总结使用jquery实现了博客日历效果.代码如下: 原文链接转载请注明:http:/ ...
随机推荐
- seajs简记
参考seajs快速入门 一.前端模块化的价值 解决命名冲突 摆脱文件依赖 性能优化 提高可维护性 seajs.use方法调用通过exports暴露接口通过require引入依赖 二.Sea.js 的常 ...
- L3-005. 垃圾箱分布
L3-005. 垃圾箱分布 题目链接:https://www.patest.cn/contests/gplt/L3-005 Dijstra 与L2-001.紧急救援类似,是Dijstra最短路的拓展, ...
- 2016腾讯we大会的时间——2016年11月6日
作为腾讯公司主办的一场国际化创新盛会,WE大会由腾讯公司自2013年以来每年举办,WE大会已迎来第四年,每年大会都会邀请全球互联网思想家.前沿创新团队来进行现场演讲和分享,分享最前沿的思想和技术,创造 ...
- php基础(二)数组
本文主要内容来自w3cschool 在 PHP 中,有三种数组类型: 索引数组 - 带有数字索引的数组 关联数组 - 带有指定键的数组 多维数组 - 包含一个或多个数组的数组 PHP 索引数组 有两种 ...
- POJ 3537 Crosses and Crosses(SG/还未想完全通的一道SG)
题目链接 #include<iostream> #include<cstdio> #include<cstring> using namespace std; ]; ...
- JS之链式运动,及任意值运动框架,包括透明度的改变
链式运动,顾名思义,一环扣一环,即执行完一个事件后,再接着执行下一个事件,在参数上面动手脚,将下一个要执行的函数名作为一个参数,即利用到回掉函数,下面代码中出现的fn,即回掉函数, function ...
- HDU 1009 FatMouse' Trade(贪心)
FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...
- Dot Product
These are vectors: They can be multiplied using the "Dot Product" (also see Cross Product) ...
- 希望获取到页面中所有的checkbox怎么做?
var domList = document.getElementsByTagName(‘input’); var checkBoxList = []; var len = domList.lengt ...
- 第二次冲刺spring会议(第六次会议)
[例会时间]2014/5/8 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳 奇