本文实例讲述了C#判断一天、一年已经过了百分之多少的方法。分享给大家供大家参考。具体如下:
这里写了四个函数,分别是
1.判断当前时间过了今天的百分之多少 static double DayTimePassed()
2.判断指定时间过了当日的百分之多少 static double DayTimePassed(DateTime dt)
3.判断当前时间过了今年的百分之多少 static double YearTimePassed()
4.判断指定时间过了当年的百分之多少 static double YearTimePassed(DateTime dt)
一、函数代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
/// <summary> /// 判断当前时间过了今天的百分之多少 /// </summary> /// <returns></returns> static double DayTimePassed() { DateTime dt = DateTime.Now; double Numerator = dt.Hour * 60 * 60 + dt.Minute * 60 + dt.Second; double Denominator = 24 * 60 * 60; return Numerator / Denominator; } /// <summary> /// 判断指定时间过了当日的百分之多少 /// </summary> /// <param name="dt"></param> /// <returns></returns> static double DayTimePassed(DateTime dt) { double Numerator = dt.Hour * 60 * 60 + dt.Minute * 60 + dt.Second; double Denominator = 24 * 60 * 60; return Numerator / Denominator; } /// <summary> /// 判断当前时间过了今年的百分之多少 /// </summary> /// <returns></returns> static double YearTimePassed() { DateTime dt = DateTime.Now; double Numerator = dt.DayOfYear * 24 * 60 * 60 + dt.Hour * 60 * 60 + dt.Minute * 60 + dt.Second; double Denominator = (DateTime.IsLeapYear(dt.Year) ? 366 : 365) * 24 * 60 * 60; return Numerator / Denominator; } /// <summary> /// 判断指定时间过了当年的百分之多少 /// </summary> /// <param name="dt"></param> /// <returns></returns> static double YearTimePassed(DateTime dt) { double Numerator = dt.DayOfYear * 24 * 60 * 60 + dt.Hour * 60 * 60 + dt.Minute * 60 + dt.Second; double Denominator = (DateTime.IsLeapYear(dt.Year) ? 366 : 365) * 24 * 60 * 60; return Numerator / Denominator; } |
二、Main函数调用
1
2
3
4
5
6
7
8
9
|
static void Main( string [] args) { Console.WriteLine(DateTime.Now.ToString()); Console.WriteLine( "Day: " + DayTimePassed().ToString( "P" )); Console.WriteLine( "Day: " + DayTimePassed(DateTime.Now).ToString( "P" )); Console.WriteLine( "Year: " + YearTimePassed().ToString( "P" )); Console.WriteLine( "Year: " + YearTimePassed(DateTime.Now).ToString( "P" )); Console.ReadLine(); } |
三、运行结果
希望本文所述对大家的C#程序设计有所帮助。