Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace ZC. Utils
{
public class Timehelper
{
<summary>
Calculation time passed a few days
</summary>
<param name= "Dtime" > Past time </param>
<returns></returns>
public static int Subday (DateTime dtime)
{
Return DateTime.Now.Subtract (Dtime). Days;
}
<summary>
Whether today
</summary>
<param name= "Dtime" ></param>
<returns></returns>
public static bool IsToDay (DateTime dtime)
{
Return dtime. Date = = DateTime.Now.Date;
}
<summary>
The specified date is the week ordinal of the year
</summary>
<param name= "Dtime" > Given date </param>
<returns> number of weeks in a year </returns>
public static int WeekOfYear (DateTime dtime)
{
Try
{
Determine the position of this time in a year
int dayofyear = Dtime. DayOfYear;
The first day of the year
DateTime tempdate = new DateTime (dtime. Year, 1, 1);
Determine the first day of the year
int tempdayofweek = (int) Tempdate.dayofweek;
Tempdayofweek = Tempdayofweek = = 0? 7:tempdayofweek;
Determine the day of the week
int index = (int) dtime. DayOfWeek;
index = index = = 0? 7:index;
Scope of the current week
DateTime retstartday = dtime. AddDays (-(index-1));
DateTime retendday = dtime. AddDays (6-index);
Determine which week is currently in the first
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>
Ask how many weeks a year
</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;
}
Day of the week based on year of month
public static string Caculateweekday (int month, Int. day)
{
String weekstr = String. Empty;
int year = DateTime.Now.Year;
Think of January and February as the January or March and January or April of the previous year.
if (month = = 1) {month = year--;}
if (month = = 2) {month = +; 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>
Get the appropriate number of weeks based on 2 time periods
</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;
Determine the position of this time in a year
int dayofyear = Begindate.dayofyear;
The first day of the year
DateTime tempdate = new DateTime (Begindate.year, Begindate.month, Begindate.day);
Last day
DateTime tempenddate = new DateTime (Enddate.year, Enddate.month, Enddate.day);
int tempdayofweek = (int) Tempdate.dayofweek;
Tempdayofweek = Tempdayofweek = = 0? 7:tempdayofweek;
Determine the day of the week
int startindex = (int) Begindate.dayofweek;
startindex = startindex = = 0? 7:startindex;
Scope of the current week
DateTime Retstartday = begindate.adddays (-(startindex-1));
DateTime retendday = begindate.adddays (7-startindex);
Determine which week is currently in the first
int weekindex = (int) math.ceiling ((double) dayofyear + tempDayOfWeek-1)/7);
return weeks;
}
<summary>
Based on the start time, get the week
</summary>
<param name= "Dtime" > Current time </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 ();
Determine the position of this time in a year
int dayofyear = ts. Days;
The first day of the year
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;
Determine the day of the week
int index = (int) dtime. DayOfWeek;
index = index = = 0? 7:index;
Scope of the current week
DateTime retstartday = dtime. AddDays (-(index-1));
DateTime retendday = dtime. AddDays (7-index);
Determine which week is currently in the first
int weekindex = (int) math.ceiling ((double) dayofyear + Tempdayofweek)/7);
return weekindex;
}
<summary>
According to the week, the week gets the specific month day
</summary>
<param name= "Week" > Weeks </param>
<param name= "Day" > Days of the Week </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 value > = 0, because of the enumeration reason, Sunday ranked first, at this time sunday-monday=-1, must +7=6.
TimeSpan ts = new TimeSpan (i, 0, 0, 0);
Get Monday of Nth Week
Sometime = Sometime.subtract (ts). AddDays ((week-1) * 7);
Get the day of the week
Sometime = Sometime.adddays (day-1);
return sometime;
}
}
}
. NET date-time helper Class (ii)