본문 바로가기
프로그래밍/JAVA, .NET, 기타

c# 달력 구현하기 - 참고 해서 더 멋지게 만들어 보세요.

by [바가지] 2019. 5. 16.
반응형

 

 

조금 오래 된 소스이지만 달력 구현 소스 입니다. 필요하면 참고해서 더 멋지게 응용해서 사용 해 보세요.

 

cs소스

private void SetCalendar()
{
  firstDay = new DateTime(year, month, 1); 
  lastDay = new DateTime(2000, 1, 1);
            
  if (month != 12)
    lastDay = (new DateTime(year, month + 1, 1)).AddDays(-1);
  else
    lastDay = new DateTime(year, 12, 31);

  if (day > lastDay.Day)
    day = lastDay.Day;

  hidFirstDay.Value = firstDay.ToString();
  hidLastDay.Value = lastDay.ToString();

  //현재 날       

  DateTime objToday = 

    new DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, System.DateTime.Now.Day);
  DateTime objCompDay = new DateTime(year, month, 1); 

  int preLastDay = firstDay.AddDays(-1).Day; //이전달의 마지막일
  int intCount;                                        //달력 줄바꿈
  int intFirstDayOfWeek = 1;                      //첫번째날 요일 
  int intLastDayOfWeek = 1;                      //마지막날 요일

  intFirstDayOfWeek = GetDayOfWeekInt(firstDay.DayOfWeek); //현재달 첫번째 요일(정수) 
  intLastDayOfWeek = GetDayOfWeekInt(lastDay.DayOfWeek);  //현재달 마지막 요일(정수)

  DateTime objStartDay = firstDay.AddDays(-intFirstDayOfWeek);         //달력에 표시 될 시작 일 
  DateTime objEndDay = lastDay.AddDays(7 - intLastDayOfWeek + 6);  //테달력에 표시 될 마지막 일 
  DateTime objCurrentDay = objStartDay;                                       //Loop 현재 날짜

  HtmlTableRow htblRow;
  HtmlTableCell htblCell;
  htblRow = new HtmlTableRow();

   //달력 루프
   for (intCount = 0; !objEndDay.Equals(objCurrentDay); intCount++)
   {
      if (intCount != 0 && intCount % 7 == 0)
      {
         htblCalendar.Rows.Add(htblRow);
         htblRow = new HtmlTableRow();
      }

      //생성
      htblCell = new HtmlTableCell();

      objCurrentDay = objStartDay.AddDays(intCount);

      if (objToday.Equals(objCurrentDay)) //금일
         htblCell.BgColor = "색상표코드입력";
      else //오늘이 아닐경우
         htblCell.BgColor = "색상표코드입력";

      if (objCompDay.Month == objCurrentDay.Month)
      {
          if (objToday.Equals(objCurrentDay)) //금일
          {
             htblCell.InnerHtml = "" + objCurrentDay.Day.ToString() + "";
             htblCell.Style["cursor"] = "hand";
             htblCell.Attributes["onclick"] = 

                 "SelectValue('" + textBoxName + "','" + objCurrentDay.Date.ToString("yyyy-MM-dd") + "');";
          }
          else if (objCurrentDay.DayOfWeek == DayOfWeek.Sunday) //일요일
          {
              htblCell.InnerHtml = "" + objCurrentDay.Day.ToString() + "";
              htblCell.Style["cursor"] = "hand";
              htblCell.Attributes["onclick"] = 

                 "SelectValue('" + textBoxName + "','" + objCurrentDay.Date.ToString("yyyy-MM-dd") + "');";
           }
           else if (objCurrentDay.DayOfWeek == DayOfWeek.Saturday) //토요일
           {
              htblCell.InnerHtml = "" + objCurrentDay.Day.ToString() + "";
              htblCell.Style["cursor"] = "hand";
              htblCell.Attributes["onclick"] 

                 = "SelectValue('" + textBoxName + "','" + objCurrentDay.Date.ToString("yyyy-MM-dd") + "');";
           }
           else
           {
              htblCell.InnerHtml = "" + objCurrentDay.Day.ToString() + "";
              htblCell.Style["cursor"] = "hand";
              htblCell.Attributes["onclick"] 

                 = "SelectValue('" + textBoxName + "','" + objCurrentDay.Date.ToString("yyyy-MM-dd") + "');";
           }
        }
        else //현재 월이 아닐경우
        {
           htblCell.InnerHtml = "" + objCurrentDay.Day.ToString() + "";
           htblCell.Style["cursor"] = "hand";
           htblCell.Attributes["onclick"] 

              = "SelectValue('" + textBoxName + "','" + objCurrentDay.Date.ToString("yyyy-MM-dd") + "');";
        }

         htblCell.Height = "100";  //높이지정
         htblCell.Align = "center";
         htblRow.Cells.Add(htblCell);
      }

      htblCalendar.Rows.Add(htblRow);
  }

 


aspx소스

[table id="Table18" height="25" cellspacing="0" cellpadding="1" width="199" bgcolor="beige" border="0"]
     [tr]
          [td align="right" width="1"]                                                                                                       

              [asp:button id="btnPrevYear" runat="server" text="◀"][/asp:button]

          [/td]
          [td align="left" width="1"]

              [asp:button id="btnPrevMonth" runat="server" text="◁"][/asp:button]

          [/td]
          [td align="middle"]

               [asp:label id="lblDate" runat="server"]2003년 9월[/asp:label]

          [/td]
          [td align="right" width="1"]

               [asp:button id="btnNextMonth" runat="server" text="▷"][/asp:button]

          [/td]
          [td align="left" width="1"]

               [asp:button id="btnNextYear" runat="server" text="▶"][/asp:button]

          [/td]
      [/tr]
[/table]
[table id="htblCalendar" cellspacing="1" cellpadding="4" border="0" runat="server"]
       [tr]
           [td]일[/td]
           [td]월[/td]
           [td]화[/td]
           [td]수[/td]
           [td]목[/td]
           [td]금[/td]
           [td]토[/td]
        [/tr]
 [/table]

 

달력 이전달, 다음달 등 기능의 cs소스는 포함하지 않았습니다. 참고하세요.

이상 c# 닷넷 환경에서 달력 만들기였습니다.

반응형