C\#달력 을 간단하게 출력 하 는 방법

4218 단어 C#일력
본 고 는 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\#프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기