Skip to content

Instantly share code, notes, and snippets.

@Myoga1012
Last active January 30, 2023 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Myoga1012/fa9712f81a00b03fd910 to your computer and use it in GitHub Desktop.
Save Myoga1012/fa9712f81a00b03fd910 to your computer and use it in GitHub Desktop.
TypeScriptでカレンダーを出力するソースコードです。
// Visual StudioでそのTypeScriptファイルをビルドした時に生成されるJavaScriptコードです。
window.onload = function () {
// 現在日を取得し、当月1日の曜日と末日を求めます。
var now = new Date();
var prePad = new Date(now.getFullYear(), now.getMonth(), 1).getDay();
var lastDay = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate();
// カレンダーを出力します。
var el = document.getElementById('content');
for (var curDay = -prePad + 1; curDay <= lastDay; curDay++) {
// 半角スペース1つ出力します。curDayが負の時はさらに2つ出力します。
el.innerHTML += "&nbsp;" + (curDay < 1 ? "&nbsp;&nbsp;" : (curDay < 10 ? "&nbsp;" : "") + curDay.toString() + ((prePad + curDay) % 7 == 0) ? "<br/>" : "");
}
};
//# sourceMappingURL=app.js.map
// 名前 : Myoga Screw-bright (旧名:Myoga S. Tomonaka)
// Twitter : https://twitter.com/Myoga1012
window.onload = () => {
// 現在日を取得し、当月1日の曜日と末日を求めます。
var now: Date = new Date();
var prePad: number = new Date( now.getFullYear(), now.getMonth(), 1 ).getDay();
var lastDay: number = new Date( now.getFullYear(), now.getMonth() + 1, 0 ).getDate();
// カレンダーを出力します。
var el = document.getElementById( 'content' );
for ( var curDay: number = -prePad + 1; curDay <= lastDay; curDay++ ) {
// 半角スペース1つ出力します。curDayが負の時はさらに2つ出力します。
el.innerHTML += "&nbsp;" + ( curDay < 1 ? "&nbsp;&nbsp;" :
// curDayが正の時です。1桁なら半角スペースを1つ出力します。そして日付を出力します。
( curDay < 10 ? "&nbsp;" : "" ) + curDay.toString() +
// 土曜日を出力したら、改行タグを入れます。
( ( ( prePad + curDay ) % 7 == 0 ) ? "<br/>" : "" ) );
}
};
// Calendar.ts
// Copyright (c) 2014 Myoga-TN.net All Rights Reserved.
// This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php
@Myoga1012
Copy link
Author

HTMLではcontentのIDを持つタグのフォントは等幅フォントにしておきます。

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