用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. String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的区别

    string.IsNullOrEmpty 这个是判断字符串是否为:null或者string.Empty或者“”,但不包含空格 .如果是如"\t"或者“   ” 这样的字符就返回fa ...

  2. @InitBinde的作用

    @InitBinder用于在@Controller中标注于方法,表示为当前控制器注册一个属性编辑器或者其他,只对当前的Controller有效

  3. php 随笔

    <?php header("content-type:text/html;charset=utf8");class Car {   var  $name = '汽车';    ...

  4. mysql 批量修改表前缀

    直接贴码: SELECT a.*, concat( 'alter table ', a.TABLE_NAME, ' rename ge_', SUBSTR( a.TABLE_NAME FROM INS ...

  5. (转)J2EE中13个规范

       今天在做连接oracle数据库的时候,感受到了什么是规范.平时听到别人说学习j2ee一定要学习他的十三个规范,大概的知道每个规范是做什么的,每个“接口”是做什么的.          很早就听过 ...

  6. POJ 1067 取石子游戏(威佐夫博弈)

    传送门 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  7. hdu_5878_I Count Two Three(预处理)

    题目链接:hdu_5878_I Count Two Three 题意: 给你一个n,让你找满足那个式子的不比n小的最小数 题解: 先上个预处理,然后二分查找就行 #include<bits/st ...

  8. art中的部分内容,留着慢慢研究

    root@hbg:/tmp# cat /proc/mtddev:    size   erasesize  namemtd0: 00040000 00010000 "u-boot" ...

  9. wuzhi 五指 伪静态

    rewrite ^(.*)list\/([0-9]+)-([0-9]+)\.html$ $1index.php?v=listing&cid=$2&page=$3 last; rewri ...

  10. [译] Block 小测验

    本文来源于 ParseBlog 的其中一篇博文 <Objective-C Blocks Quiz> 如果您觉得我的博客对您有帮助,请通过关注我的新浪微博  MicroCai 支持我,谢谢! ...