using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleCalendar
{
public class Program
{
public static void Main(string[] args)
{
int year, month, day;
Console.Write("請輸入年份:");
year = Convert.ToInt32(Console.ReadLine());
Console.Write("請輸入月份:");
month = Convert.ToInt32(Console.ReadLine());
Console.Write("請輸入日期:");
day = Convert.ToInt32(Console.ReadLine());
Console.Clear();
DateTime d = new DateTime(year, month, day);
Console.WriteLine("{0}/{1}/{2} {3} {4}", d.Year, d.Month, d.Day, d.DayOfWeek, DateTime.DaysInMonth(d.Year, d.Month));
PrintLine();
PrintRows(3, new string[] { "日", "一", "二", "三", "四", "五", "六" }, 7);
PrintLine();
PrintRows(4, GetDaysArray(d), 7);
PrintLine();
Console.ReadKey();
}
private static string[] GetDaysArray(DateTime dt)
{
List<string> list = new List<string>();
//Fill the spaces of first line
DateTime firstDay = new DateTime(dt.Year, dt.Month, 1);
switch (firstDay.DayOfWeek)
{
case DayOfWeek.Monday:
list.Add("");
break;
case DayOfWeek.Tuesday:
list.AddRange(new string[] { "", "" });
break;
case DayOfWeek.Wednesday:
list.AddRange(new string[] { "", "", "" });
break;
case DayOfWeek.Thursday:
list.AddRange(new string[] { "", "", "", "" });
break;
case DayOfWeek.Friday:
list.AddRange(new string[] { "", "", "", "", "" });
break;
case DayOfWeek.Saturday:
list.AddRange(new string[] { "", "", "", "", "", "" });
break;
}
//the date, 1 ~ 28 or 29 or 30 or 31
//mark the date for special color
for (int i = 1; i <= DateTime.DaysInMonth(dt.Year, dt.Month); i++)
{
if (i == dt.Day)
list.Add("{" + i + "}");
else
list.Add(i + "");
}
return list.ToArray();
}
private static void PrintLine()
{
Console.WriteLine(new string('-', 36));
}
private static void PrintRows(int width, string[] columns, int itemsPerLine)
{
StringBuilder sb = new StringBuilder();
int totalLines = columns.Length % itemsPerLine == 0 ? columns.Length / itemsPerLine : columns.Length / itemsPerLine + 1;
for (int i = 0; i < totalLines; i++)
{
for (int j = 0; j < itemsPerLine; j++)
{
int pos = i * itemsPerLine + j;
if (pos >= columns.Length)
break;
sb.Append("|" + AlignCentre(columns[pos], width));
}
sb.Append("|");
sb.Append("\n");
}
//Start to print
uint STD_OUTPUT_HANDLE = 0xfffffff5;
IntPtr hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
foreach (char c in sb.ToString().ToCharArray())
{
if (c == '{')
{
//SetConsoleTextAttribute(hConsole, 12);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(' ');
continue;
}
if (c == '}')
{
//SetConsoleTextAttribute(hConsole, 7);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(' ');
continue;
}
Console.Write(c);
}
}
private static string AlignCentre(string text, int width)
{
if (string.IsNullOrEmpty(text))
return new string(' ', width);
else
return text.PadLeft(width - (width - text.Length) / 2).PadRight(width);
}
[DllImport("kernel32.dll")]
private static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, int wAttributes);
[DllImport("kernel32.dll")]
private static extern IntPtr GetStdHandle(uint nStdHandle);
}
}
Result:


Source Code:
Download