一个简单的程序,自己写着玩玩,O(∩_∩)O哈哈哈~ 没有用C#系统提供的DataTime之类的,使用了就方便多了。。。
代码如下所示:
using UnityEngine;
using System.Collections;
using System;
/// <summary>
/// 已知1900年一月一日星期一
/// </summary>
public class WeekTest : MonoBehaviour
{
public int Year;
public int Mouth;
public int Day;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
int AllDays = coutDays(Year, Mouth, Day);
int t = ((AllDays % 7)) % 7;
Debug.Log(Year + "/" + Mouth + "/" + Day + " 星期" + t);
}
}
/// <summary>
/// 查看是否是闰年
/// </summary>
bool isRunNian(int _year)
{
if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0)
{
return true;
}
return false;
}
/// <summary>
/// 一年中某一个月多少天
/// </summary>
int MouthDays(int _year,int _mouth)
{
switch (_mouth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (isRunNian(_year))
return 29;
else
return 28;
}
return 0;
}
/// <summary>
/// 从某年到某年共多少天
/// </summary>
int daysYear(int yearOne, int yearTwo)
{
int dayCout = 0;
for (int i = yearOne; i< yearTwo; i++)
{
if (isRunNian(i))
{
dayCout += 366;
}
else
{
dayCout += 365;
}
}
return dayCout;
}
/// <summary>
/// 从某年一月到某月多少天
/// </summary>
int daysMouth(int _year, int _mouth)
{
int day = 0;
for (int i = 1; i < _mouth; i++)
{
day += MouthDays(_year, i);
}
return day;
}
/// <summary>
/// 从1900年到某日期共多少天
/// </summary>
int coutDays(int _year,int _Mouth,int _day)
{
int days = 0;
days = daysYear(1900, _year) + daysMouth(_year, _Mouth) + _day;
return days;
}
}
测试如下所示: