C#简单输出日历的方法
作者:dongfengkuayue 时间:2023-11-13 05:56:10
本文实例讲述了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
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,日历
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解C# 结构体
2023-09-30 15:37:19
java 多态与抽象类详解总结
2022-04-19 10:59:18
![](https://img.aspxhome.com/file/2023/5/67135_0s.jpg)
.NET企业级项目中遇到的国际化问题和解决方法
2022-12-03 08:18:06
Android 在viewPager中双指缩放图片双击缩放图片单指拖拽图片的实现思路
2021-12-09 14:04:22
详解Java的MyBatis框架和Spring框架的整合运用
2022-09-19 19:22:20
java获取当前时间的四种方法代码实例
2023-11-28 19:22:53
Java实现的Base64加密算法示例
2023-10-29 00:37:08
![](https://img.aspxhome.com/file/2023/8/58618_0s.jpg)
Java设计模式之工厂模式(Factory模式)介绍
2021-10-15 11:13:15
![](https://img.aspxhome.com/file/2023/4/79164_0s.png)
Android Selector和Shape的使用方法
2022-08-05 23:48:38
![](https://img.aspxhome.com/file/2023/7/106057_0s.gif)
Android编程之selector下设置背景属性值的方法
2023-08-23 08:07:49
springboot集成mybatis plus和dynamic-datasource注意事项说明
2023-12-05 03:54:21
JavaScript嵌入百度地图API的最详细方法
2023-04-12 14:33:03
![](https://img.aspxhome.com/file/2023/4/133604_0s.jpg)
Javassist用法详解
2023-06-26 17:11:54
详解Android中Runtime解决屏幕旋转问题(推荐)
2022-04-03 03:12:02
RocketMQ producer发送者浅析
2023-04-03 06:35:32
C# 定时器定时更新的简单实例
2023-01-08 12:45:50
使用Spring Data Redis实现数据缓存的方法
2021-08-02 10:19:25
![](https://img.aspxhome.com/file/2023/6/71706_0s.png)
Spring创建bean实例的几种方式分享
2022-02-21 22:08:19
java后端+前端使用WebSocket实现消息推送的详细流程
2022-04-24 12:34:53
![](https://img.aspxhome.com/file/2023/1/94371_0s.png)
c# List和Dictionary常用的操作
2023-06-14 15:44:32