Date Format Conversion-months, weeks, and days

Source: Internet
Author: User

/// <Summary>
/// Year, month, week, and day String Array
/// </Summary>
/// <Param name = "dt"> date to be converted </param>
/// <Returns> </returns>
Public string [] getYearMonthWeekDayOfDate (DateTime dt)
{
String [] YearMonthWeekDay = new string [4];
YearMonthWeekDay [0] = dt. Year. ToString ();
YearMonthWeekDay [1] = dt. Month. ToString ();
YearMonthWeekDay [2] = "W" + getWeekNumberOfMonth (dt). ToString ();
YearMonthWeekDay [3] = dt. Day. ToString ();
Return YearMonthWeekDay;
}
/// <Summary>
/// Obtain the week number of the date
/// </Summary>
/// <Param name = "dt"> date to be converted </param>
/// <Returns> </returns>
Public int getWeekNumberOfMonth (DateTime dt)
{
DateTime monthStartDate = DateTime. Parse (dt. Year. ToString () + "-" + dt. Month. ToString () + "-" + "01 ");
DateTime monthEndDate = monthStartDate. AddMonths (1 );
Int difference = getWeekDifference (monthStartDate. DayOfWeek. ToString (), "Sunday ");
If (dt <= monthStartDate. AddDays (difference) & dt <= monthEndDate)
{
Return 1;
}
Else if (dt <monthStartDate. AddDays (difference + 7 + 1) & dt <monthEndDate)
{
Return 2;
}
Else if (dt <monthStartDate. AddDays (difference + 7*2 + 1) & dt <monthEndDate)
{
Return 3;
}
Else if (dt <monthStartDate. AddDays (difference + 7*3 + 1) & dt <monthEndDate)
{
Return 4;
}
Else if (dt <monthStartDate. AddDays (difference + 7*4 + 1) & dt <monthEndDate)
{
Return 5;
}
Else
{
Return 6;
}
}
/// <Summary>
/// If the result is less than-7 or greater than 7, the input parameter is incorrect.
/// </Summary>
/// <Param name = "start"> start DayOfWeek </param>
/// <Param name = "end"> end DayOfWeek </param>
/// <Returns> </returns>
Public int getWeekDifference (string start, string end)
{
Int difference = getWeek (end)-getWeek (start );
Return difference;
}
/// <Summary>
/// Convert DayOfWeek to week
/// </Summary>
/// <Param name = "strWeek"> DayOfWeek </param>
/// <Returns> </returns>
Public week getWeek (string strWeek)
{
Switch (strWeek)
{
Case "Sunday ":
Return week. Sunday;
Case "Monday ":
Return week. Monday;
Case "Tuesday ":
Return week. Tuesday;
Case "Wednesday ":
Return week. Wednesday;
Case "Thursday ":
Return week. Thursday;
Case "Friday ":
Return week. Friday;
Case "Saturday ":
Return week. Saturday;
Default:
Return week. Wrong;
}
}
Public enum week
{
/// <Summary>
/// Sunday
/// </Summary>
Sunday = 7,
/// <Summary>
/// Monday
/// </Summary>
Monday = 1,

/// <Summary>
/// Tuesday
/// </Summary>
Tuesday = 2,

/// <Summary>
/// Wednesday
/// </Summary>
Wednesday = 3,

/// <Summary>
/// Thursday
/// </Summary>
Thursday = 4,

/// <Summary>
/// Friday
/// </Summary>
Friday = 5,

/// <Summary>
/// Saturday
/// </Summary>
Saturday = 6,
/// <Summary>
///
/// </Summary>
Wrong = 0,
}

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.