Skip to content

Instantly share code, notes, and snippets.

@Myoga1012
Last active January 30, 2023 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Myoga1012/575548d5c96366411f96 to your computer and use it in GitHub Desktop.
Save Myoga1012/575548d5c96366411f96 to your computer and use it in GitHub Desktop.
【旧バージョン】F#でカレンダーを出力するソースコードです。
// 名前 : Myoga Screw-bright (旧名:Myoga S. Tomonaka)
// Twitter : https://twitter.com/Myoga1012
open System;
[<EntryPoint>]
let main argv =
// 現在の日付を取得し、当月1日の曜日と末日を求めます。
let now = DateTime.Today
let prePad = int( DateTime( now.Year, now.Month, 1 ).DayOfWeek )
let lastDay = DateTime.DaysInMonth( now.Year, now.Month )
// カレンダーを出力します。
for curDay in [( -prePad + 1 )..lastDay] do
// 範囲を[( - 1日の曜日 + 1 )..末日]にし、要素が負の時に空白を出力します。
// こうすることで1日の曜日に合わせてオフセットすることができます。
if curDay > 0 then printf "%3d" curDay else printf " "
if ( curDay + prePad ) % 7 = 0 || curDay = lastDay then printfn ""
done
0
// Calender.fs
// Copyright (c) 2014 Myoga-TN.net All Rights Reserved.
// This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php
// 旧コード
open System;
[<EntryPoint>]
let main argv =
let now = DateTime.Today
let firstWeek = int( DateTime( now.Year, now.Month, 1 ).DayOfWeek )
let lastDay = DateTime.DaysInMonth( now.Year, now.Month )
for i = 1 to firstWeek do printf " " done
for day in [1..lastDay] do
printf "%3d" day
if ( day + firstWeek ) % 7 = 0 then printfn ""
else if ( day = lastDay ) then printfn ""
done
0
@Myoga1012
Copy link
Author

月末が土曜日だと、最後に1行空いてしまうので、条件文で回避させました。

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