C#簡(jiǎn)單輸出日歷的方法
本文實(shí)例講述了C#簡(jiǎn)單輸出日歷的方法。分享給大家供大家參考。具體如下:
用C#輸出日歷,此功能可用于Ajax方式列出計(jì)劃日程相關(guān)的內(nèi)容,由于是C#控制輸出,可以方便加上自己需要的業(yè)務(wù)處理邏輯。
1.控制臺(tái)輸出:
using System;
namespace 控制臺(tái)日歷
{
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;//獲取當(dāng)年當(dāng)月1號(hào)的星期
//Console.WriteLine("當(dāng)月一號(hào)的星期{0}",week1);
int lastday = day1.AddMonths(1).AddDays(-1).Day; //獲取當(dāng)月的最后一天
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:月份偏量,用來(lái)查看上一月下一月
/// </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;//獲取當(dāng)年當(dāng)月1號(hào)的星期
int lastday = day1.AddMonths(1).AddDays(-1).Day; //獲取當(dāng)月的最后一天
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
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#實(shí)現(xiàn)日歷效果
- C#實(shí)現(xiàn)中文日歷Calendar
- c# 日歷控件的實(shí)現(xiàn)
- C#?日歷類(lèi)功能的實(shí)例
- C#實(shí)現(xiàn)漂亮的數(shù)字時(shí)鐘效果
- c# winform時(shí)鐘的實(shí)現(xiàn)代碼
- C#日歷樣式的下拉式計(jì)算器實(shí)例講解
- C#實(shí)現(xiàn)功能強(qiáng)大的中國(guó)農(nóng)歷日歷操作類(lèi)
- C#實(shí)現(xiàn)農(nóng)歷日歷的方法
- C#實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘和日歷
相關(guān)文章
WPF自定義實(shí)現(xiàn)上傳文件顯示進(jìn)度的按鈕控件
自定義控件在WPF開(kāi)發(fā)中是很常見(jiàn)的,有時(shí)候某些控件需要契合業(yè)務(wù)或者美化統(tǒng)一樣式,這時(shí)候就需要對(duì)控件做出一些改造,本文就來(lái)自定義實(shí)現(xiàn)一個(gè)上傳文件顯示進(jìn)度的按鈕控件吧2023-06-06
.net實(shí)現(xiàn)文件讀寫(xiě)的幾種常用方法
這篇文章主要介紹了.net實(shí)現(xiàn)文件讀寫(xiě)的幾種常用方法,非常實(shí)用,需要的朋友可以參考下2014-08-08
c# 通過(guò)內(nèi)存映射實(shí)現(xiàn)文件共享內(nèi)存的示例代碼
這篇文章主要介紹了c# 通過(guò)內(nèi)存映射實(shí)現(xiàn)文件共享內(nèi)存的示例代碼,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-04-04
C#實(shí)現(xiàn)DataTable數(shù)據(jù)行列轉(zhuǎn)換
這篇文章介紹了C#實(shí)現(xiàn)DataTable數(shù)據(jù)行列轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
C#連續(xù)任務(wù)Task.ContinueWith方法
這篇文章介紹了C#中的連續(xù)任務(wù)Task.ContinueWith方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
C# 中SharpMap的簡(jiǎn)單使用實(shí)例詳解
SharpMap是一個(gè)基于.net 2.0使用C#開(kāi)發(fā)的Map渲染類(lèi)庫(kù),可以渲染各類(lèi)GIS數(shù)據(jù)(目前支持ESRI Shape和PostGIS格式),可應(yīng)用于桌面和Web程序,具體內(nèi)容詳情大家參考下本文吧2017-08-08

