Last active
January 30, 2023 08:52
-
-
Save Nia-TN1012/2e8879f50efb4bad14a15774838c877d to your computer and use it in GitHub Desktop.
Razorを使ってCalendar出力するソースコードです。コードの言語はC#です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
名前 : Nia Tomonaka | |
Twitter : https://twitter.com/nia_tn1012 | |
--> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Calendar on Razor ( C# )</title> | |
</head> | |
<body> | |
@* 現在の日付を取得から当月1日を表すインスタンスを生成します。 *@ | |
@{DateTime now1 = new DateTime( DateTime.Today.Year, DateTime.Today.Month, 1 ); } | |
<h1>@now1.ToString( "Y", new System.Globalization.CultureInfo( "en-us" ) )</h1> @* 現在の年月を表示します。 *@ | |
@* // 当月1日からその曜日分だけ引きます。 *@ | |
@{DateTime curDay = now1.AddDays( -( int )now1.DayOfWeek );} | |
<p style="font-family:'MS Gothic'"> | |
@{ | |
do { | |
// 日付を出力します、但し、出力先がHTMLなので、Replaceメソッドで半角スペースを「 」に変換し、 | |
// Html.Rawメソッドを使って「 」がHTMLの特殊文字として解釈されるようにします。 | |
// 参考資料 : http://blogs.msdn.com/b/chack/archive/2011/01/02/html-encode-asp-net-mvc-3-with-razor-html-raw-and-htmlstring.aspx | |
// もし、curDayが前月であれば、 ×3を出力します。 | |
// こうすることで1日の曜日に合わせてオフセットすることができます。 | |
if( curDay >= now1 ) { <text>@Html.Raw( string.Format( "{0, 3}", curDay.Day ).Replace( " ", " " ) )</text> } else {<text> </text> } | |
// dayが土曜日または月末日であれば、改行します。 | |
// curDayが土曜日または当月末日であれば、改行します。 | |
if( curDay.DayOfWeek == DayOfWeek.Saturday || | |
curDay.Month == now1.Month && curDay.Day == DateTime.DaysInMonth( now1.Year, now1.Month ) ) {<br />}; | |
// curDayを1日分進めて、翌月になるまで繰り返します。 | |
} while( ( curDay = curDay.AddDays( 1.0 ) ).Month == now1.Month || curDay <= now1 ); | |
} | |
</p> | |
</body> | |
</html> | |
<!-- | |
Calender.cshtml( Razor statement with C# ) | |
Copyright (c) 2014-2023 Nia T.N. Tech Lab. / Chronoir.net. | |
This software is released under the MIT License. | |
http://opensource.org/licenses/mit-license.php | |
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Calendar on Razor ( C# )</title> | |
</head> | |
<body> | |
<h1>October 2014</h1> | |
<p style="font-family:'MS Gothic'"> | |
1 2 3 4 <br /> 5 6 7 8 9 10 11 <br /> 12 13 14 15 16 17 18 <br /> 19 20 21 22 23 24 25 <br /> 26 27 28 29 30 31 <br /> | |
</p> | |
<!-- Visual Studio Browser Link --> | |
<script type="application/json" id="__browserLink_initializationData"> | |
{"appName":"Internet Explorer","requestId":"bc9af4e738ac4e16960e9fd031f8aec0"} | |
</script> | |
<script type="text/javascript" src="http://localhost:57832/e6baf7ffbda441ac9415bbb70f7c2a0e/browserLink" async="async"></script> | |
<!-- End Browser Link --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GitHubのアカウント統合のため、Myoga1012→Nia-TN1012に移行しました。
旧URL: https://gist.github.com/Myoga1012/d874ad00cea91d082dcd