C # computing time-determine the week by date and determine the date range by week

Source: Internet
Author: User

Using System;
Using System. Globalization;
Namespace ConvertDateTime
{
/// <Summary>
/// ConvertDateTime returns a brief description of the specified parameters.

/// </Summary>
Public class ConvertDate
{
Public ConvertDate ()
{}
//************************************** *****************************//
# Region determines the current day of the week based on the current date
Public static string GetWeekDay (string strDate)
{
Try
{
// Time To be determined
DateTime dTime = Convert. ToDateTime (strDate );

Return GetWeekDay (dTime );
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}

}
Public static string GetWeekDay (DateTime dTime)
{
Try
{
// Determine the day of the week
Int index = (int) dTime. DayOfWeek;
Return GetWeekDay (index );
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}

}
# Endregion

//************************************** *****************************//
# Region obtain the maximum number of weeks in the current year
Public static int GetMaxWeekOfYear (int year)
{
Try
{
DateTime tempDate = new DateTime (year, 12, 31 );
Int tempDayOfWeek = (int) tempDate. DayOfWeek;
If (tempDayOfWeek! = 0)
{
TempDate = tempDate. Date. AddDays (-tempDayOfWeek );
}
Return GetWeekIndex (tempDate );
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}
}
Public static int GetMaxWeekOfYear (DateTime dTime)
{
Try
{
Return GetMaxWeekOfYear (dTime. Year );
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}
}
# Endregion

//************************************** *****************************//
# Region obtain the week number based on the time

// If the year December 31 and the year January 1 are the same week, the first week of the year is counted.
Public static int GetWeekIndex (DateTime dTime)
{
Try
{
// Time To be determined
// DateTime dTime = Convert. ToDateTime (strDate );
// Determine the position of the time in one year
Int dayOfYear = dTime. DayOfYear;

// DateTime tempDate = new DateTime (dTime. Year, 1, 6, calendar );
// The first day of the current year
DateTime tempDate = new DateTime (dTime. Year, 1, 1 );

// Confirm the first day of the current year
Int tempDayOfWeek = (int) tempDate. DayOfWeek;
TempDayOfWeek = 0? 7: tempDayOfWeek;
// Determine the day of the week
Int index = (int) dTime. DayOfWeek;

Index = 0? 7: index;

// The range of the current week
DateTime retStartDay = dTime. AddDays (-(index-1 ));
DateTime retEndDay = dTime. AddDays (7-index );

// Determine the week
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 );
}
// String retVal = retStartDay. ToString ("yyyy/MM/dd") + "~ "+ RetEndDay. ToString (" yyyy/MM/dd ");

}
Public static int GetWeekIndex (string strDate)
{
Try
{
// Time To be determined
DateTime dTime = Convert. ToDateTime (strDate );
Return GetWeekIndex (dTime );
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}

}
# Endregion

//************************************** *****************************//
# Region obtain the date range of the week based on the time

Public static string GetWeekRange (string strDate)
{
Try
{
// Time To be determined
DateTime dTime = Convert. ToDateTime (strDate );
Return GetWeekRange (dTime );
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}
}
Public static string GetWeekRange (DateTime dTime)
{
Try
{
Int index = (int) dTime. DayOfWeek;

Index = 0? 7: index;

// The range of the current week
DateTime retStartDay = dTime. AddDays (-(index-1 ));
DateTime retEndDay = dTime. AddDays (7-index );

Return WeekRangeToString (retStartDay, retEndDay );
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}

}

Public static string GetWeekRange (int year, int weekIndex)
{
Try
{
If (weekIndex <1)
{
Throw new Exception ("enter an integer greater than 0 ");
}

Int allDays = (weekIndex-1) * 7;
// Confirm the first day of the current year
DateTime firstDate = new DateTime (year, 1, 1 );
Int firstDayOfWeek = (int) firstDate. DayOfWeek;

FirstDayOfWeek = 0? 7: firstDayOfWeek;

// Week start day
Int startAddDays = allDays + (1-firstDayOfWeek );
DateTime weekRangeStart = firstDate. AddDays (startAddDays );
// End day of the week
Int endAddDays = allDays + (7-firstDayOfWeek );
DateTime weekRangeEnd = firstDate. AddDays (endAddDays );

If (weekRangeStart. Year> year |
(WeekRangeStart. Year = year & weekRangeEnd. Year> year ))
{
Throw new Exception ("No Week" + weekIndex + "this year. ");
}

Return WeekRangeToString (weekRangeStart, weekRangeEnd );
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}
}
Public static string GetWeekRange (int weekIndex)
{
Try
{
Return GetWeekRange (DateTime. Now. Year, weekIndex );
}
Catch (Exception ex)
{
Throw new Exception (ex. Message );
}
}

Private static string WeekRangeToString (DateTime weekRangeStart, DateTime weekRangeEnd)
{
String strWeekRangeStart = weekRangeStart. ToString ("yyyy/MM/dd ");
String strWeekRangeend = weekRangeEnd. AddDays. ToString ("yyyy/MM/dd ");

Return strWeekRangeStart + "~ "+ StrWeekRangeend;

}
# Endregion

//************************************** *****************************//
# Region expression of the week

Private static string GetWeekDay (int index)
{
String retVal = string. Empty;
Switch (index)
{
Case 0:
{
RetVal = "Sunday ";
Break;
}
Case 1:
{
RetVal = "Monday ";
Break;
}
Case 2:
{
RetVal = "Tuesday ";
Break;
}
Case 3:
{
RetVal = "Wednesday ";
Break;
}
Case 4:
{
RetVal = "Thursday ";
Break;
}
Case 5:
{
RetVal = "Friday ";
Break;
}
Case 6:
{
RetVal = "Saturday ";
Break;
}
}

Return retVal;
}
# Endregion
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.