Skip to content

Instantly share code, notes, and snippets.

@Nia-TN1012
Forked from Myoga1012/Calendar.cshtml
Last active January 30, 2023 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nia-TN1012/2e8879f50efb4bad14a15774838c877d to your computer and use it in GitHub Desktop.
Save Nia-TN1012/2e8879f50efb4bad14a15774838c877d to your computer and use it in GitHub Desktop.
Razorを使ってCalendar出力するソースコードです。コードの言語はC#です。
<!--
名前 : 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メソッドで半角スペースを「&nbsp;」に変換し、
// Html.Rawメソッドを使って「&nbsp;」が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が前月であれば、&nbsp;×3を出力します。
// こうすることで1日の曜日に合わせてオフセットすることができます。
if( curDay >= now1 ) { <text>@Html.Raw( string.Format( "{0, 3}", curDay.Day ).Replace( " ", "&nbsp;" ) )</text> } else {<text>&nbsp;&nbsp;&nbsp;</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
-->
<!DOCTYPE html>
<html>
<head> 
<title>Calendar on Razor ( C# )</title>
</head>
<body>
<h1>October 2014</h1>
<p style="font-family:'MS Gothic'">
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;1 &nbsp;&nbsp;2 &nbsp;&nbsp;3 &nbsp;&nbsp;4 <br /> &nbsp;&nbsp;5 &nbsp;&nbsp;6 &nbsp;&nbsp;7 &nbsp;&nbsp;8 &nbsp;&nbsp;9 &nbsp;10 &nbsp;11 <br /> &nbsp;12 &nbsp;13 &nbsp;14 &nbsp;15 &nbsp;16 &nbsp;17 &nbsp;18 <br /> &nbsp;19 &nbsp;20 &nbsp;21 &nbsp;22 &nbsp;23 &nbsp;24 &nbsp;25 <br /> &nbsp;26 &nbsp;27 &nbsp;28 &nbsp;29 &nbsp;30 &nbsp;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>
@Nia-TN1012
Copy link
Author

GitHubのアカウント統合のため、Myoga1012→Nia-TN1012に移行しました。

旧URL: https://gist.github.com/Myoga1012/d874ad00cea91d082dcd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment