用C#输出日历,此功能可用于Ajax方式列出计划日程相关的内容,由于是C#控制输出,可以方便加上自己需要的业务处理逻辑。

1.控制台输出:

  1. using System;
  2. namespace 控制台日历
  3. {
  4. class Program
  5. {
  6. public static void Main(string[] args)
  7. {
  8. string s = "    ";
  9. Console.WriteLine("输入年份:");
  10. int nYear = int.Parse(Console.ReadLine());
  11. Console.WriteLine("输入月份:");
  12. int nMonth = int.Parse(Console.ReadLine());
  13. DateTime day1 = new DateTime(nYear,nMonth,1);
  14. Console.WriteLine("{0}/{1}",day1.Year,day1.Month);
  15. Console.WriteLine("日  一  二  三  四  五  六");
  16. int week1 =(int )day1.DayOfWeek;//获取当年当月1号的星期
  17. //Console.WriteLine("当月一号的星期{0}",week1);
  18. int lastday = day1.AddMonths(1).AddDays(-1).Day; //获取当月的最后一天
  19. for (int i = 0; i < week1; i++)
  20. Console.Write(s);//不能换行输出
  21. for (int i = 1; i <= lastday; i++)
  22. {
  23. Console.Write("{0:00}  ", i);//按01 02   输出
  24. if ((i + week1) % 7 == 0)
  25. Console.WriteLine();
  26. }
  27. Console.WriteLine();
  28. Console.Write("Press any key to continue . . . ");
  29. Console.ReadKey(true);
  30. }
  31. }
  32. }

效果图:

2.Html表格输出:

  1. #region 生成表格日历
  2. /// <summary>
  3. /// 生成表格日历 index:月份偏量,用来查看上一月下一月
  4. /// </summary>
  5. /// <param name="index"></param>
  6. /// <returns></returns>
  7. public static string GetCalendarHtml(int index = 0)
  8. {
  9. DateTime day1 = new DateTime(DateTime.Now.AddMonths(index).Year, DateTime.Now.AddMonths(index).Month, 1);
  10. int week1 = (int)day1.DayOfWeek;//获取当年当月1号的星期
  11. int lastday = day1.AddMonths(1).AddDays(-1).Day; //获取当月的最后一天
  12. System.Text.StringBuilder builder = new System.Text.StringBuilder();
  13. 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));
  14. builder.Append("<tr class='calendar_head'>");
  15. builder.Append("<td class='calendar_cell'>日</td>");
  16. builder.Append("<td class='calendar_cell'>一</td>");
  17. builder.Append("<td class='calendar_cell'>二</td>");
  18. builder.Append("<td class='calendar_cell'>三</td>");
  19. builder.Append("<td class='calendar_cell'>四</td>");
  20. builder.Append("<td class='calendar_cell'>五</td>");
  21. builder.Append("<td class='calendar_cell'>六</td>");
  22. builder.Append("</tr>");
  23. string emptyString = "<td class='calendar_cell'> </td>";
  24. if (week1 > 0)
  25. {
  26. builder.Append("<tr class='calendar_body'>");
  27. for (int i = 0; i < week1; i++)
  28. {
  29. builder.Append(emptyString);
  30. }
  31. }
  32. for (int i = 1; i <= lastday; i++)
  33. {
  34. string day = string.Format("{0:00}  ", i);//按01 02   输出
  35. builder.Append(string.Format("<td class='calendar_cell'>{0}</td>", day));
  36. if ((i + week1) % 7 == 0)
  37. {
  38. builder.Append("</tr><tr class='calendar_body'>");
  39. }
  40. }
  41. builder.Append("</tr>");
  42. builder.Append("</table>");
  43. return builder.ToString();
  44. }
  45. #endregion

页面前台结合javascript实现ajax日历切换效果,只需用js改变函数中的index偏移量即可。

C#输出日历的更多相关文章

  1. JS输出日历

    页面HTML代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...

  2. PHP程序输出日历

    以下代码只是简单实现日历的效果和逻辑思路,没有使用类封装,权当抛砖引玉,有兴趣的朋友可以封装起来,方便调用. <?php /** * PHP利用时间函数输出日历 * Rain.zen $ int ...

  3. C语言 · 输出日历

    算法提高 输出日历   时间限制:1.0s   内存限制:512.0MB      按照下述格式打印2006年12月日历: Calendar 2006-12---------------------- ...

  4. Java输出日历写法

    package TestString_2; import java.text.ParseException;import java.util.Calendar;import java.util.Gre ...

  5. Java输出日历

    源码链接:http://pan.baidu.com/s/1o6xeybK

  6. Python学习实践-----打印日历

    使用python语言实现在控制台打印日历 输入年.月.日 输出对应日历,指定的日数输出为'--' 程序没有做严格的输入验证,故输入整数即可. 以下为没有优化的源码: print_calendar.py ...

  7. javascript js写特效日历

    <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 创建一个js日历(原生JS实现日历)

    前言 方法是有参考网上一些资料的,比如闰年的判断,比如每个月第一天是星期几的判断.不说太多,拆分出一个个函数,希望能描述尽可能的清晰明了. 一,判断闰年 //判断闰年 function runNian ...

  9. .net mvc页面UI之Jquery博客日历控件

    摘要:最近在做一个博客系统,其他需要用到博客日历控件,网上搜索了很多资料,其中大部分都是javascript的,经过总结使用jquery实现了博客日历效果.代码如下: 原文链接转载请注明:http:/ ...

随机推荐

  1. js数组对象方法

  2. MySQL5.5安装出现CMake错误找不到CMakelists.txt原因

    今天虚拟机上测试安装 CentOS6.3 + PHP5.4.8 + MySQL5.5.28,结果捣鼓了半天 MySQL都没装上,老是CMake目录下找不到那个 lists 文件,郁闷的不行,最后发现问 ...

  3. hdu 1298 T9

    字典树+DFS. #include<cstdio> #include<cstring> #include<cmath> #include<string> ...

  4. 《Windows编程循序渐进》——建立MFC应用程序

    如何建立MFC应用程序 打开VS2013:

  5. awstats 日志分析

    /tmp/awstats/awstats.ezrydel.com.conf LogFile="/usr/local/apache/domlogs/ezrydel.com" php版 ...

  6. 创建简单的MVC项目

    一,新建一个空的MVC项目 二,连接数据库,添加一个实体模型Model.edmx

  7. Linux入门(四)linux运行环境mysql详细操作及安装phpmyadmin

    1.1 安装mysql(中间需要设定数据库的密码) sudo apt-get install mysql-serversudo apt-get install php5-mysql   #安装php5 ...

  8. Linux入门(二)Linux基本命令及基本操作

    1 常用Linux命令 图形界面进入到字符界面: ctrl+alt+F2~F6 字符界面进入到图形界面:ctrl +alt+F7 查看本机ip:  ifconfig  (windows是:ipconf ...

  9. hdu_5950_Recursive sequence(矩阵快速幂)

    题目链接:hdu_5950_Recursive sequence 题意:递推求解:F(n) = 2*F(n-2) + F(n-1) + n4 和F(1) = a,F(2) = b: 题解: 一看数据范 ...

  10. netty初探(1)

    参考目录: 1. user-guide : http://netty.io/wiki/user-guide-for-4.x.html 2. demo: http://netty.io/wiki/ 3. ...