在.net项目中,对DateTime的相关操作使用的比较多,例如时间格式的转换,时间间隔的计算,时间所属的区间计算等,在这些要求中,虽然使用起来较为的简单,但是在转换的过程中,较为容易出错,花费的时间也较多,现在总结一些常用的时间操作方法,以便在项目开发中节省时间。
一下代码中需要使用到如下的实体类:
/// <summary>
/// 周(星期)信息实体类
/// </summary>
[Serializable]
public class WeekInfo
{
/// <summary>
/// 周(星期)信息实体类
/// </summary>
public WeekInfo()
{
Number = 0;
BeginDate = DateTime.MinValue;
EndDate = DateTime.MaxValue;
}
/// <summary>
/// 周数
/// </summary>
public int Number { get; set; }
/// <summary>
/// 开始时间
/// </summary>
public DateTime BeginDate { get; set; }
/// <summary>
/// 结束时间
/// </summary>
public DateTime EndDate { get; set; }
/// <summary>
/// 输出第几周和日期间隔
/// <para>默认格式:</para>
/// <para>第几周,从2012年12月21日至2112年12月21日。</para>
/// </summary>
/// <returns>第几周和日期间隔</returns>
public override string ToString()
{
return string.Format("第{0}周,从{1}至{2}。", Number, BeginDate.ToString("yyyy年MM月dd日"), EndDate.ToString("yyyy年MM月dd日"));
}
/// <summary>
/// 输出第几周
/// </summary>
/// <param name="isFill">少于2位时是否补零</param>
/// <returns>第几周</returns>
public string GetWeekString(bool isFill)
{
string format = "第{0}周";
if (isFill && Number < 10) format = "第0{0}周";
return string.Format(format, Number);
}
/// <summary>
/// 输出日期间隔
/// </summary>
/// <param name="inputString">输出格式化字符串</param>
/// <param name="dateString">日期格式化字符串</param>
/// <returns>日期间隔</returns>
public string GetDateString(string inputString, string dateString)
{
if (string.IsNullOrWhiteSpace(inputString) || string.IsNullOrWhiteSpace(dateString)) return null;
try
{
return string.Format(inputString, BeginDate.ToString(dateString), EndDate.ToString(dateString));
}
catch (Exception ex)
{
return ex.Message;
}
}
}
1.获得年度第一个周一的日期:
/// <summary>
/// 获得年度第一个周一的日期
/// </summary>
/// <param name="year">需要计算的年份</param>
/// <param name="jumpYear">年度第一周是否跳过跨年的周数</param>
/// <param name="offset">年度第一个周一偏移量</param>
/// <returns>年度第一个周一的日期</returns>
public static DateTime WeekOfFirstDay(int year, bool jumpYear, out int offset)
{
var firstDate = YearOfFirstDay(year); //该年的第一天
var firstWeek = (int)firstDate.DayOfWeek; //该年的第一天是周几
offset = 0; //周一偏移量
switch (firstWeek)
{
case 1: //星期一
offset = 0;
break;
case 2: //星期二
offset = jumpYear ? 6 : -1;
break;
case 3: //星期三
offset = jumpYear ? 5 : -2;
break;
case 4: //星期四
offset = jumpYear ? 4 : -3;
break;
case 5: //星期五
offset = jumpYear ? 3 : -4;
break;
case 6: //星期六
offset = jumpYear ? 2 : -5;
break;
case 0: //星期日
offset = jumpYear ? 1 : -6;
break;
}
firstDate = firstDate.AddDays(offset);
return firstDate;
}
2.获得年度周(星期)信息实体集合列表:
/// <summary>
/// 获得年度周(星期)信息实体集合列表
/// </summary>
/// <param name="year">需要计算的年份</param>
/// <param name="jumpYear">年度第一周是否跳过跨年的周数</param>
/// <returns>周(星期)信息实体集合列表对象</returns>
public static IList<WeekInfo> WeekOfList(int year, bool jumpYear)
{
IList<WeekInfo> weekList = new List<WeekInfo>();
if (year <= 0001 || year >= 9999) return weekList;
var offset = 0;
var firstDate = WeekOfFirstDay(year, jumpYear, out offset); //年度周一的日期
var index = 1;
while (true)
{
if (index > 54) break;
var weekInfo = new WeekInfo
{
Number = index,
BeginDate = firstDate,
EndDate = firstDate.AddDays(6)
};
weekList.Add(weekInfo);
firstDate = firstDate.AddDays(7); //下周
if (jumpYear)
{
if (firstDate.Year != year) break;
}
else
{
if (firstDate.AddDays(6).Year != year) break;
}
index++;
}
return weekList;
}
3.获得某年第某周的开始日期和结束日期:
/// <summary>
/// 获得某年第某周的开始日期和结束日期
/// </summary>
/// <param name="year">需要计算的年份</param>
/// <param name="weekNumber">需要计算的周数</param>
/// <param name="jumpYear">年度第一周是否跳过跨年的周数</param>
/// <param name="weekBeginDate">开始日期</param>
/// <param name="weekEndDate">结束日期</param>
public static void WeekOfDate(int year, int weekNumber, bool jumpYear, out DateTime weekBeginDate, out DateTime weekEndDate)
{
weekBeginDate = DateTime.MinValue;
weekEndDate = DateTime.MaxValue;
if (year <= 0001 || year >= 9999 || weekNumber < 1 || weekNumber > 54) return;
int offset;
var firstDate = WeekOfFirstDay(year, jumpYear, out offset); //年度周一的日期
firstDate = firstDate.AddDays((weekNumber - 1) * 7);
weekBeginDate = firstDate;
weekEndDate = firstDate.AddDays(6);
}
4.获得某个日期属于某年的第几周:
/// <summary>
/// 获得某个日期属于某年的第几周
/// </summary>
/// <param name="currentDate">需要计算的日期</param>
/// <param name="jumpYear">年度第一周是否跳过跨年的周数</param>
/// <returns>某年的第几周</returns>
public static int WeekOfCurrent(DateTime currentDate, bool jumpYear)
{
if (DateTime.MinValue == currentDate || DateTime.MaxValue == currentDate) return 0;
int result, offset;
var firstDate = WeekOfFirstDay(currentDate.Year, jumpYear, out offset); //年度周一的日期
var firstWeek = (int)firstDate.DayOfWeek; //该年的第一天是周几
var currentDay = currentDate.DayOfYear;
if (offset > 0)
{
currentDay += offset;
}
else
{
currentDay -= offset;
}
var remainderDay = currentDay - (7 - firstWeek);
if (remainderDay < 1)
{
result = 1;
}
else
{
result = remainderDay / 7;
if (remainderDay % 7 != 0) result++;
result++;
}
return result;
}
5.统计一段时间内有多少个星期几:
/// <summary>
/// 统计一段时间内有多少个星期几
/// </summary>
/// <param name="beginDate">开始日期</param>
/// <param name="endDate">结束日期</param>
/// <param name="weekNumber">星期几</param>
/// <returns>多少个星期几</returns>
public static int WeekOfTotalWeeks(DateTime beginDate, DateTime endDate, DayOfWeek weekNumber)
{
var dayTotal = new TimeSpan(endDate.Ticks - beginDate.Ticks);
var result = (int)dayTotal.TotalDays / 7;
var iLen = dayTotal.TotalDays % 7;
for (var i = 0; i <= iLen; i++)
{
if (beginDate.AddDays(i).DayOfWeek != weekNumber) continue;
result++;
break;
}
return result;
}
6.计算当前月属于第几季度:
/// <summary>
/// 计算当前月属于第几季度
/// </summary>
/// <returns>当前年第几季度</returns>
public static int QuarterOfCurrent()
{
return QuarterOfCurrent(DateTime.Now.Month);
}
/// <summary>
/// 计算某个月属于第几季度
/// </summary>
/// <param name="month">需要计算的月份</param>
/// <returns>某年第几季度</returns>
public static int QuarterOfCurrent(int month)
{
if (month < 1 || month > 12) return -1;
return (month - 1) / 3 + 1;
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。