C#时间操作类时间计算等_.docx

上传人:PIYPING 文档编号:11650692 上传时间:2021-08-28 格式:DOCX 页数:10 大小:13.58KB
返回 下载 相关 举报
C#时间操作类时间计算等_.docx_第1页
第1页 / 共10页
C#时间操作类时间计算等_.docx_第2页
第2页 / 共10页
C#时间操作类时间计算等_.docx_第3页
第3页 / 共10页
C#时间操作类时间计算等_.docx_第4页
第4页 / 共10页
C#时间操作类时间计算等_.docx_第5页
第5页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《C#时间操作类时间计算等_.docx》由会员分享,可在线阅读,更多相关《C#时间操作类时间计算等_.docx(10页珍藏版)》请在三一文库上搜索。

1、C#时间操作类时间计算等_ 一个C#时间工具类,可用于时间计算,比如依据诞生年月实现生日提示、两个日期天数比较、依据英文的星期几返回中文的星期几、计算两个时间的差值,返回的是x天x小时x分钟x秒、时间相差值、依据时间返回几个月前,几天前,几小时前,几分钟前以及几秒前,现在用不到的话,先保藏吧,代码如下: 001using System; 002using System.Collections.Generic; 003using System.Linq; 004using System.Text; 005namespace CLB.Utility.CharTools 006 007 public

2、 static class DateTimeHelper 008 009 / 010 / 依据时间返回几个月前,几天前,几小时前,几分钟前以及几秒前 011 / 012 / 013 / 014 public static string DateStringFromNow(DateTime dt) 015 016 TimeSpan span = DateTime.Now - dt; 017 if (span.TotalDays 60) 018 019 return dt.ToShortDateString(); 020 021 else if (span.TotalDays 30) 022 02

3、3 return 1个月前; 024 025 else if (span.TotalDays 14) 026 027 return 2周前; 028 029 else if (span.TotalDays 7) 030 031 return 1周前; 032 033 else if (span.TotalDays 1) 034 035 return string.Format(0天前, (int)Math.Floor(span.TotalDays); 036 037 else if (span.TotalHours 1) 038 039 return string.Format(0小时前, (

4、int)Math.Floor(span.TotalHours); 040 041 else if (span.TotalMinutes 1) 042 043 return string.Format(0分钟前, (int)Math.Floor(span.TotalMinutes); 044 045 else if (span.TotalSeconds = 1) 046 047 return string.Format(0秒前, (int)Math.Floor(span.TotalSeconds); 048 049 else 050 051 return 1秒前; 052 053 054 / 0

5、55 / 时间相差值,返回时间差 056 / 调用时,isTotal为true时,返回的时带小数的天数,否则返回的是整数 057 / 058 / 059 / 060 / 061 / 062 public static string DateDiff(DateTime DateTime1, DateTime DateTime2, bool isTotal) 063 064 TimeSpan ts = DateTime2 - DateTime1; 065 if (isTotal) 066 /带小数的天数,比如1天12小时结果就是1.5 067 return ts.TotalDays.ToStrin

6、g(); 068 else 069 /整数天数,1天12小时或者1天20小时结果都是1 070 return ts.Days.ToString(); 071 072 / 073 / 计算两个时间的差值,返回的是x天x小时x分钟x秒 074 / 075 / 076 / 077 / 078 public static string DateDiff(DateTime DateTime1, DateTime DateTime2) 079 080 string dateDiff = null; 081 TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); 082

7、TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); 083 TimeSpan ts = ts1.Subtract(ts2).Duration(); 084 /TimeSpan ts=ts1.Add(ts2).Duration(); 085 dateDiff = ts.Days.ToString() + 天 + ts.Hours.ToString() + 小时 + ts.Minutes.ToString() + 分钟+ ts.Seconds.ToString() + 秒; 086 return dateDiff; 087 088 / 089 / 依据英文的

8、星期几返回中文的星期几 090 / 如WhichDay(Sunday),返回星期日 091 / 092 / 093 / 094 public static string WhichDay(string enWeek) 095 096 switch (enWeek.Trim() 097 098 case Sunday: 099 return 星期日; 100 case Monday: 101 return 星期一; 102 case Tuesday: 103 return 星期二; 104 case Wednesday: 105 return 星期三; 106 case Thursday: 10

9、7 return 星期四; 108 case Friday: 109 return 星期五; 110 case Saturday: 111 return 星期六; 112 default: 113 return enWeek; 114 115 116 / 117 / 日期比较 118 / 119 / 当前日期 120 / 输入日期 121 / 比较天数 122 / 大于天数返回true,小于返回false 123 public static bool CompareDate(string today, string writeDate, int n) 124 125 DateTime Toda

10、y = Convert.ToDateTime(today); 126 DateTime WriteDate = Convert.ToDateTime(writeDate); 127 WriteDate = WriteDate.AddDays(n); 128 if (Today = WriteDate) 129 return false; 130 else 131 return true; 132 133 / 134 / 依据诞生年月进行生日提示 135 / 136 / 137 / 138 public static string GetBirthdayTip(DateTime birthday

11、) 139 140 DateTime now = DateTime.Now; 141 /TimeSpan span = DateTime.Now - birthday; 142 int nowMonth = now.Month; 143 int birtMonth = birthday.Month; 144 if (nowMonth = 12 birtMonth = 1) 145 return string.Format(下月0号, birthday.Day); 146 if (nowMonth = 1 birtMonth = 12) 147 return string.Format(上月0号

12、, birthday.Day); 148 int months = now.Month - birthday.Month; 149 /int days = now.Day - birthday.Day; 150 if (months = 1) 151 return string.Format(上月0号, birthday.Day); 152 else if (months = -1) 153 return string.Format(下月0号, birthday.Day); 154 else if (months = 0) 155 156 if (now.Day = birthday.Day) 157 return 今日; 158 return string.Format(本月0号, birthday.Day); 159 160 else if (months 1) 161 return string.Format(已过0月, months); 162 else 163 return string.Format(0月1日, birthday.Month, birthday.Day); 164 165 166 更多信息请查看IT技术专栏 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1