2017年12月5日 星期二

[程式類]C# 產生HTML的行事曆格式 how to create a calendar in html.

有時只是想要在網頁上顯示一個月有哪些內容~並不是真正的要搞個行事曆系統出來, 此時可以用用這個,只是產出HTML語法; 丟到client端上的某個table 的 innerHTML 就可以了~

this function just generates the HTML of a calendar.  Using <table> <tr><td> tag.



public string GetCalendarDetail(string tmpYear, string tmpMonth)
{
string tmpStr = "";

tmpYear = "2017";
tmpMonth = "12";

int MonthDayCount = DateTime.DaystmpMonth(int.Parse(tmpYear), int.Parse(tmpMonth));

tmpStr = "<table class='table table-bordered my_table my_class_table'><tr style='height:13px;font-size:16px;'>";

int i, j, tmpDay = 1;
DateTime tmpDate;
int StartWeekDay = Convert.ToInt16(Convert.ToDateTime(tmpYear + "/" + tmpMonth + "/01").DayOfWeek);
tmpDay = tmpDay - StartWeekDay;
for (j = 1; j <= StartWeekDay; j++)
{
tmpStr += "<td></td>";
}
j = j - 1;
for (i = 1; i <= MonthDayCount; i++)
{
//Head
tmpDate = Convert.ToDateTime(tmpYear + "/" + tmpMonth + "/" + i);
if ((i + j) % 7 == 1)
{
tmpStr += "</tr>";
//Body
if (StartWeekDay == 0 && i == 1)
tmpStr = tmpStr.Replace("<tr style='height:13px;font-size:16px;'></tr>", "");
else
{
tmpStr += "<tr style='height:70px;font-size:12px;'>";
for (int k = 1; k <= 7; k++, tmpDay++)
{
tmpStr += "<td>";
if (tmpDay >= 1 && tmpDay <= MonthDayCount)
{
//tmpDaty is the Date
tmpStr += tmpDay.ToString();
}
tmpStr += "</td>";
}
tmpStr += "</tr>";
}
}
tmpStr += "<td>" + tmpDate.ToString("MM/dd") + "(" + tmpDate.DayOfWeek.ToString() + ")</td>";
}

//fill the qup
if (i + StartWeekDay <= 36)
for (int x = i + StartWeekDay; x <= 35; x++)
tmpStr += "<td></td>";
else if (i + StartWeekDay > 36)
for (int x = i + StartWeekDay; x <= 42; x++)
tmpStr += "<td></td>";

tmpStr += "<tr style='height:70px;font-size:12px;'>";
for (int k = 1; k <= 7; k++, tmpDay++)
{
tmpStr += "<td>";
if (tmpDay >= 1 && tmpDay <= MonthDayCount)
{
//tmpDaty is the Date
tmpStr += tmpDay.ToString();
}
tmpStr += "</td>";
}
tmpStr += "</tr>";

tmpStr += "</td></table>";

return tmpStr;
}

沒有留言:

張貼留言

POSTGRE SQL 想使用變數去定義一段日期區間, 但遍尋不到像是在 MSSQL 時常用的 DECLARE @count int, @x int, @y nvarchar(10)... 想到一招利用 CTE 方式去定義一個這樣類似變數的作法來實現... 原來的script w...