using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ZC.Utils
{
public class TimeHelper
{
/// <summary>
/// 计算时间过去了几天
/// </summary>
/// <param name="dtime">过去时间</param>
/// <returns></returns>
public static int SubDay(DateTime dtime)
{
return DateTime.Now.Subtract(dtime).Days;
}
/// <summary>
/// 是否今天
/// </summary>
/// <param name="dtime"></param>
/// <returns></returns>
public static bool IsToDay(DateTime dtime)
{
return dtime.Date == DateTime.Now.Date;
}
/// <summary>
/// 取指定日期是一年中的第几周
/// </summary>
/// <param name="dtime">给定的日期</param>
/// <returns>数字 一年中的第几周</returns>
public static int WeekOfYear(DateTime dtime)
{
try
{
//确定此时间在一年中的位置
int dayOfYear = dtime.DayOfYear;
//当年第一天
DateTime tempDate = new DateTime(dtime.Year, 1, 1);
//确定当年第一天
int tempDayOfWeek = (int)tempDate.DayOfWeek;
tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
////确定星期几
int index = (int)dtime.DayOfWeek;
index = index == 0 ? 7 : index;
//当前周的范围
DateTime retStartDay = dtime.AddDays(-(index - 1));
DateTime retEndDay = dtime.AddDays(6 - index);
//确定当前是第几周
int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7);
if (retStartDay.Year < retEndDay.Year)
{
weekIndex = 1;
}
return weekIndex;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 求某年有多少周
/// </summary>
/// <param name="dtime"></param>
/// <returns></returns>
public static int GetWeekOfYear(DateTime dtime)
{
int countDay = DateTime.Parse(dtime.Year + "-12-31").DayOfYear;
int countWeek = countDay / 7;
return countWeek;
}
//根据年月日获得星期几
public static string CaculateWeekDay(int month, int day)
{
string weekstr = string.Empty;
int year = DateTime.Now.Year;
//把一月和二月看成是上一年的十三月和十四月
if (month == 1) { month = 13; year--; }
if (month == 2) { month = 14; year--; }
int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
switch (week)
{
case 0: weekstr = "1"; break;
case 1: weekstr = "2"; break;
case 2: weekstr = "3"; break;
case 3: weekstr = "4"; break;
case 4: weekstr = "5"; break;
case 5: weekstr = "6"; break;
case 6: weekstr = "7"; break;
}
return weekstr;
}
/// <summary>
/// 根据2个时间段获得相应的周数
/// </summary>
/// <param name="beginDate"></param>
/// <param name="endDate"></param>
/// <returns></returns>
public static int WeekOfDate(DateTime beginDate, DateTime endDate)
{
TimeSpan ts1 = new TimeSpan(beginDate.Ticks);
TimeSpan ts2 = new TimeSpan(endDate.Ticks);
TimeSpan ts = ts2.Subtract(ts1).Duration();
int weeks = ts.Days / 7;
//确定此时间在一年中的位置
int dayOfYear = beginDate.DayOfYear;
//当年第一天
DateTime tempDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day);
//最后一天
DateTime tempendDate = new DateTime(endDate.Year, endDate.Month, endDate.Day);
int tempDayOfWeek = (int)tempDate.DayOfWeek;
tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
////确定星期几
int startindex = (int)beginDate.DayOfWeek;
startindex = startindex == 0 ? 7 : startindex;
//当前周的范围
DateTime retStartDay = beginDate.AddDays(-(startindex - 1));
DateTime retEndDay = beginDate.AddDays(7 - startindex);
//确定当前是第几周
int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7);
return weeks;
}
/// <summary>
/// 根据起始时间,获取第几周
/// </summary>
/// <param name="dtime">当前时间</param>
/// <returns></returns>
public static int WeekOfTermDate(DateTime dtime)
{
string datetime = "2011-3-1";
TimeSpan ts1 = new TimeSpan(dtime.Ticks);
TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(datetime).Ticks);
TimeSpan ts = ts2.Subtract(ts1).Duration();
//确定此时间在一年中的位置
int dayOfYear = ts.Days;
//当年第一天
DateTime tempDate = new DateTime(Convert.ToDateTime(datetime).Year, Convert.ToDateTime(datetime).Month, Convert.ToDateTime(datetime).Day);
int tempDayOfWeek = (int)tempDate.DayOfWeek;
tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
////确定星期几
int index = (int)dtime.DayOfWeek;
index = index == 0 ? 7 : index;
//当前周的范围
DateTime retStartDay = dtime.AddDays(-(index - 1));
DateTime retEndDay = dtime.AddDays(7 - index);
//确定当前是第几周
int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek) / 7);
return weekIndex;
}
/// <summary>
/// 根据周,星期获得具体年月日
/// </summary>
/// <param name="week">第几周</param>
/// <param name="day">星期几</param>
/// <returns></returns>
public static DateTime DateTimeByWeekAndDay(int week, int day)
{
DateTime someTime = Convert.ToDateTime("2011-3-1");
int i = someTime.DayOfWeek - DayOfWeek.Monday;
if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。
TimeSpan ts = new TimeSpan(i, 0, 0, 0);
//获取第N周的星期一
someTime = someTime.Subtract(ts).AddDays((week - 1) * 7);
//获得星期几
someTime = someTime.AddDays(day - 1);
return someTime;
}
}
}