01class Program
02{
03// 定义必须变量
04const int _1M = 60; // 分钟
05const int _1H = _1M * 60; // 小时
06const int _1D = _1H * 24; // 天
07const long _1Y = _1D * 365; // 年(非闰年)
08const long _YS = _1Y * 3 + _1D * 366; // 一个闰年年度
09const long _30D = _1D * 30; // 30天(月)
10const long _31D = _1D * 31; // 31天(月)
11const long _28D = _1D * 28; // 28天(月)
12const long _29D = _1D * 29; // 29天(月)
13long[] NormalYear = { _31D, _28D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 年
14long[] LeapYear = { _31D, _29D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 闰年
15static void Main(string[] args)
16{
17Program P = new Program();
18System.Console.WriteLine(P.getDate(P.getTimeSpame()));
19DateTime T = DateTime.Now;
20System.Console.WriteLine(P.getTimeSpame() + " : " + P.getTimeSpame(T.Year, T.Month, T.Day, T.Hour, T.Minute, T.Second));
21System.Console.ReadKey();
22}
23private Program() {}
24public string getDate(long TimeSp)
25{
26// 年,月,天,小时,分钟,秒
27int year = 0;
28int month = 0;
29int day = 0;
30int hour = 0;
31int minute = 0;
32int second = 0;
33//DateTime now = DateTime.Now;
34//long TimeSp = getTimeSpame(); // 当前时间戳
35// 年
36int _y1 = (int)(TimeSp / _YS); // 获得按年度得到的年度
37TimeSp -= _YS * _y1; // 计算剩余秒
38int _y2 = (int)(TimeSp / _1Y); // 剩余年
39TimeSp -= _1Y * _y2;
40year = _y1 * 4 + _y2 + 1970;
41// 月
42long[] YearArr = isLeapYear(year) ? LeapYear : NormalYear; // 获取年的月度表
43month = 1; // 从1月开始计算
44for (int i = 0; i < YearArr.Length; i++)
45{
46if (TimeSp - YearArr[i] < 0) break;
47++month;
48TimeSp -= YearArr[i];
49}
50// 天
51day = (int)(TimeSp / _1D);
52TimeSp -= day * _1D;
53// 时
54hour = (int)(TimeSp / _1H);
55TimeSp -= hour * _1H;
56// 分
57minute = (int)(TimeSp / _1M);
58// 秒
59second = (int)(TimeSp % _1M);
60string DateStr = year + "年" + month + "月" + day + "日 " + hour + "点" + minute + "分" + second + "秒";
61return DateStr;
62}
63// 判断是否闰年
64private bool isLeapYear(int year)
65{
66return (year % 4 == 0 ? true : false);
67}
68// 获取当前时间戳 按1970年开始计算,精度为秒!
69private long getTimeSpame()
70{
71DateTime _Now = DateTime.Now;
72DateTime _1970 = new DateTime(1970, 1, 1);
73long _Sp = (_Now.Ticks - _1970.Ticks) / 10000000;
74return _Sp;
75}
76// 按既定格式把时间转成成时间戳
77private long getTimeSpame(int Year, int Month, int Day, int Hour, int Minute, int Second)
78{
79long val = 0;
80val += Second; // 秒
81val += Minute * _1M; // 分钟
82val += Hour * _1H; // 小时
83val += Day * _1D; // 天
84long[] YearArr = isLeapYear(Year) ? LeapYear : NormalYear;
85for (int i = 0; i < Month - 1; i++)
86{
87val += YearArr[i];
88}
89Year -= 1970;
90val += (Year / 4) * _YS;
91Year -= (int)(Year / 4) * 4;
92val += Year * _1Y;
93return val;
94}
95}
复制代码
class Program
{
// 定义必须变量
const int _1M = 60; // 分钟
const int _1H = _1M * 60; // 小时
const int _1D = _1H * 24; // 天
const long _1Y = _1D * 365; // 年(非闰年)
const long _YS = _1Y * 3 + _1D * 366; // 一个闰年年度
const long _30D = _1D * 30; // 30天(月)
const long _31D = _1D * 31; // 31天(月)
const long _28D = _1D * 28; // 28天(月)
const long _29D = _1D * 29; // 29天(月)
long[] NormalYear = { _31D, _28D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 年
long[] LeapYear = { _31D, _29D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 闰年
static void Main(string[] args)
{
Program P = new Program();
System.Console.WriteLine(P.getDate(P.getTimeSpame()));
DateTime T = DateTime.Now;
System.Console.WriteLine(P.getTimeSpame() + " : " + P.getTimeSpame(T.Year, T.Month, T.Day, T.Hour, T.Minute, T.Second));
System.Console.ReadKey();
}
private Program() {}
public string getDate(long TimeSp)
{
// 年,月,天,小时,分钟,秒
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
//DateTime now = DateTime.Now;
//long TimeSp = getTimeSpame(); // 当前时间戳
// 年
int _y1 = (int)(TimeSp / _YS); // 获得按年度得到的年度
TimeSp -= _YS * _y1; // 计算剩余秒
int _y2 = (int)(TimeSp / _1Y); // 剩余年
TimeSp -= _1Y * _y2;
year = _y1 * 4 + _y2 + 1970;
// 月
long[] YearArr = isLeapYear(year) ? LeapYear : NormalYear; // 获取年的月度表
month = 1; // 从1月开始计算
for (int i = 0; i < YearArr.Length; i++)
{
if (TimeSp - YearArr[i] < 0) break;
++month;
TimeSp -= YearArr[i];
}
// 天
day = (int)(TimeSp / _1D);
TimeSp -= day * _1D;
// 时
hour = (int)(TimeSp / _1H);
TimeSp -= hour * _1H;
// 分
minute = (int)(TimeSp / _1M);
// 秒
second = (int)(TimeSp % _1M);
string DateStr = year + "年" + month + "月" + day + "日 " + hour + "点" + minute + "分" + second + "秒";
return DateStr;
}
// 判断是否闰年
private bool isLeapYear(int year)
{
return (year % 4 == 0 ? true : false);
}
// 获取当前时间戳 按1970年开始计算,精度为秒!
private long getTimeSpame()
{
DateTime _Now = DateTime.Now;
DateTime _1970 = new DateTime(1970, 1, 1);
long _Sp = (_Now.Ticks - _1970.Ticks) / 10000000;
return _Sp;
}
// 按既定格式把时间转成成时间戳
private long getTimeSpame(int Year, int Month, int Day, int Hour, int Minute, int Second)
{
long val = 0;
val += Second; // 秒
val += Minute * _1M; // 分钟
val += Hour * _1H; // 小时
val += Day * _1D; // 天
long[] YearArr = isLeapYear(Year) ? LeapYear : NormalYear;
for (int i = 0; i < Month - 1; i++)
{
val += YearArr[i];
}
Year -= 1970;
val += (Year / 4) * _YS;
Year -= (int)(Year / 4) * 4;
val += Year * _1Y;
return val;
}
}