| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Linq; |
| 4 | using System.Text; |
| 5 | using System.Threading.Tasks; |
| 6 | |
| 7 | namespace Core.Librarys |
| 8 | { |
| 9 | public class Time |
| 10 | { |
| 11 | public static string ToHoursString(double seconds) |
| 12 | { |
| 13 | double hours = seconds / 60 / 60; |
| 14 | |
| 15 | if (hours > 0.1) |
| 16 | { |
| 17 | return hours.ToString("f2"); |
| 18 | } |
| 19 | else |
| 20 | { |
| 21 | return "0"; |
| 22 | } |
| 23 | } |
| 24 | public static string ToString(int seconds) |
| 25 | { |
| 26 | if (seconds < 60) |
| 27 | { |
| 28 | return seconds + "秒"; |
| 29 | } |
| 30 | else |
| 31 | { |
| 32 | double minutes = (double)seconds / 60; |
| 33 | if (minutes < 60) |
| 34 | { |
| 35 | if (minutes != (int)minutes) |
| 36 | { |
| 37 | double pointNumber = minutes - (int)minutes; |
| 38 | int seconds_ = (int)(pointNumber * 60); |
| 39 | return (int)minutes + "分钟" + (seconds_ > 0 ? seconds_ + "秒" : ""); |
| 40 | } |
| 41 | |
| 42 | return minutes + "分钟"; |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | double hours = (double)minutes / 60; |
| 47 | if (hours != (int)hours) |
| 48 | { |
| 49 | double pointNumber = hours - (int)hours; |
| 50 | int minutes_ = (int)(pointNumber * 60); |
| 51 | return (int)hours + "小时" + (minutes_ > 0 ? minutes_ + "分" : ""); |
| 52 | } |
| 53 | return hours + "小时"; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public static DateTime[] GetThisWeekDate() |
| 59 | { |
| 60 | DateTime weekStartDate = DateTime.Now, weekEndDate = DateTime.Now; |
| 61 | if (DateTime.Now.DayOfWeek == DayOfWeek.Monday) |
| 62 | { |
| 63 | weekStartDate = DateTime.Now.Date; |
| 64 | weekEndDate = DateTime.Now.Date.AddDays(6); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | int weekNum = (int)DateTime.Now.DayOfWeek; |
| 69 | if (weekNum == 0) |
| 70 | { |
| 71 | weekNum = 7; |
| 72 | } |
| 73 | weekNum -= 1; |
| 74 | weekStartDate = DateTime.Now.Date.AddDays(-weekNum); |
| 75 | weekEndDate = weekStartDate.Date.AddDays(6); |
| 76 | } |
| 77 | return new[] { weekStartDate, weekEndDate }; |
| 78 | } |
| 79 | |
| 80 | public static DateTime[] GetLastWeekDate() |
| 81 | { |
| 82 | DateTime weekStartDate = DateTime.Now, weekEndDate = DateTime.Now; |
| 83 | |
| 84 | int weekNum = (int)DateTime.Now.DayOfWeek; |
| 85 | if (weekNum == 0) |
| 86 | { |
| 87 | weekNum = 7; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | weekStartDate = DateTime.Now.Date.AddDays(-6 - weekNum); |
| 92 | weekEndDate = weekStartDate.AddDays(6); |
| 93 | |
| 94 | return new[] { weekStartDate, weekEndDate }; |
| 95 | } |
| 96 | |
| 97 | public static DateTime[] GetMonthDate(DateTime date) |
| 98 | { |
| 99 | var dateStart = new DateTime(date.Year, date.Month, 1); |
| 100 | var dateEnd = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); |
| 101 | |
| 102 | return new[] { dateStart, dateEnd }; |
| 103 | } |
| 104 | |
| 105 | public static DateTime[] GetYearDate(DateTime date) |
| 106 | { |
| 107 | var dateStart = new DateTime(date.Year, 1, 1, 0, 0, 0); |
| 108 | var dateEnd = new DateTime(date.Year, 12, DateTime.DaysInMonth(date.Year, 12), 23, 59, 59); |
| 109 | return new[] { dateStart, dateEnd }; |
| 110 | |
| 111 | } |
| 112 | } |
| 113 | } |