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:/ ...
随机推荐
- String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的区别
string.IsNullOrEmpty 这个是判断字符串是否为:null或者string.Empty或者“”,但不包含空格 .如果是如"\t"或者“ ” 这样的字符就返回fa ...
- @InitBinde的作用
@InitBinder用于在@Controller中标注于方法,表示为当前控制器注册一个属性编辑器或者其他,只对当前的Controller有效
- php 随笔
<?php header("content-type:text/html;charset=utf8");class Car { var $name = '汽车'; ...
- mysql 批量修改表前缀
直接贴码: SELECT a.*, concat( 'alter table ', a.TABLE_NAME, ' rename ge_', SUBSTR( a.TABLE_NAME FROM INS ...
- (转)J2EE中13个规范
今天在做连接oracle数据库的时候,感受到了什么是规范.平时听到别人说学习j2ee一定要学习他的十三个规范,大概的知道每个规范是做什么的,每个“接口”是做什么的. 很早就听过 ...
- POJ 1067 取石子游戏(威佐夫博弈)
传送门 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- hdu_5878_I Count Two Three(预处理)
题目链接:hdu_5878_I Count Two Three 题意: 给你一个n,让你找满足那个式子的不比n小的最小数 题解: 先上个预处理,然后二分查找就行 #include<bits/st ...
- art中的部分内容,留着慢慢研究
root@hbg:/tmp# cat /proc/mtddev: size erasesize namemtd0: 00040000 00010000 "u-boot" ...
- wuzhi 五指 伪静态
rewrite ^(.*)list\/([0-9]+)-([0-9]+)\.html$ $1index.php?v=listing&cid=$2&page=$3 last; rewri ...
- [译] Block 小测验
本文来源于 ParseBlog 的其中一篇博文 <Objective-C Blocks Quiz> 如果您觉得我的博客对您有帮助,请通过关注我的新浪微博 MicroCai 支持我,谢谢! ...