C # date help class [original]

Source: Internet
Author: User
Because the project requires a simple date help class, please forget it!
C #-code

// ================================================ ======================================
// Producnt name: boboarts. codemad
// Version 1.0
// Author: Dong Guangxiang
// Auto generated at: 2009-8-7 13:36:20
// ================================================ ======================================
Using system;

/// <Summary>
/// Date help class
/// </Summary>
Public static class Utility
{
// Day
Public static string nowtime ()
{
Return datetime. Now. year. tostring () + "-" + datetime. Now. Month. tostring () + "-" + datetime. Now. Day. tostring ();
}

// Subtract the corresponding days from the current day
Public static string gettimebydayreduce (double day)
{
Return datetime. now. year. tostring () + "-" + datetime. now. month. tostring () + "-" + datetime. now. adddays (day ). day. tostring ();
}

// Specify the first day of the month year
Public static datetime getfirstdayofmonth (INT year, int month)
{
Return convert. todatetime (Year. tostring () + "-" + month. tostring () + "-1 ");
}

// Specify the last day of the month year
Public static datetime getlastdayofmonth (INT year, int month)
{
Int days = datetime. daysinmonth (year, month );
Return convert. todatetime (Year. tostring () + "-" + month. tostring () + "-" + days. tostring ());
}

// The current week
Public static void thisweek (out string bdate, out string edate)
{
Datetime firstday = convert. todatetime (nowtime ());
Double theday;
If (firstday. dayofweek = dayofweek. Sunday) {theday = 7 ;}
Else if (firstday. dayofweek = dayofweek. Monday) {theday = 1 ;}
Else if (firstday. dayofweek = dayofweek. Tuesday) {theday = 2 ;}
Else if (firstday. dayofweek = dayofweek. Wednesday) {theday = 3 ;}
Else if (firstday. dayofweek = dayofweek. Thursday) {theday = 4 ;}
Else if (firstday. dayofweek = dayofweek. Friday) {theday = 5 ;}
Else {theday = 6 ;}
Double bday =-theday;
Double eday = 7-theday;
Bdate = firstday. adddays (bday). tostring ();
Edate = firstday. adddays (eday). tostring ();
}

// The past week
Public static void beforeweek (out string bdate, out string edate)
{

Datetime firstday = convert. todatetime (gettimebydayreduce (-thisweeklastday () + 1 ));
Double theday;
If (firstday. dayofweek = dayofweek. Sunday) {theday = 7 ;}
Else if (firstday. dayofweek = dayofweek. Monday) {theday = 1 ;}
Else if (firstday. dayofweek = dayofweek. Tuesday) {theday = 2 ;}
Else if (firstday. dayofweek = dayofweek. Wednesday) {theday = 3 ;}
Else if (firstday. dayofweek = dayofweek. Thursday) {theday = 4 ;}
Else if (firstday. dayofweek = dayofweek. Friday) {theday = 5 ;}
Else {theday = 6 ;}
Double bday =-theday;
Double eday = 7-theday;
Bdate = firstday. adddays (bday). tostring ();
Edate = firstday. adddays (eday). tostring ();
}

// Last day of the week
Public static double thisweeklastday ()
{
Datetime firstday = convert. todatetime (nowtime ());
Double theday;
If (firstday. dayofweek = dayofweek. Sunday) {theday = 7 ;}
Else if (firstday. dayofweek = dayofweek. Monday) {theday = 1 ;}
Else if (firstday. dayofweek = dayofweek. Tuesday) {theday = 2 ;}
Else if (firstday. dayofweek = dayofweek. Wednesday) {theday = 3 ;}
Else if (firstday. dayofweek = dayofweek. Thursday) {theday = 4 ;}
Else if (firstday. dayofweek = dayofweek. Friday) {theday = 5 ;}
Else {theday = 6 ;}
Return 7-theday;
}

// The first day of the month
Public static datetime getfirstdayofmonthtime ()
{
Return getfirstdayofmonth (datetime. Now. Year, datetime. Now. month );
}

// Last day of the month
Public static datetime getlastdayofmonthtime ()
{
Return getlastdayofmonth (datetime. Now. Year, datetime. Now. month );
}

// The first day of last month
Public static datetime getfirstdayofbeforemonthtime ()
{
Return getfirstdayofmonth (datetime. Now. Year, datetime. Now. Month-1 );
}

// Last day of last month
Public static datetime getlastdayofbeforemonthtime ()
{
Return getlastdayofmonth (datetime. Now. Year, datetime. Now. Month-1 );
}
// Obtain the corresponding time according to the drop-down box status
Public static void gettime (out string lastdatetime, out string firstdatetime, string state)
{
Switch (state)
{
Case "": firstdatetime = ""; lastdatetime = "";
Break;
Case "today": firstdatetime = nowtime (); lastdatetime = "";
Break;
Case "yesterday": firstdatetime = nowtime (); lastdatetime = gettimebydayreduce (-1 );
Break;
Case "last7days": firstdatetime = nowtime (); lastdatetime = gettimebydayreduce (-7 );
Break;
Case "thisweek": thisweek (Out firstdatetime, out lastdatetime );
Break;
Case "lastweek": beforeweek (Out firstdatetime, out lastdatetime );
Break;
Case "thismonth": firstdatetime = getfirstdayofmonthtime (). tostring (); lastdatetime = getlastdayofmonthtime (). tostring ();
Break;
Case "lastmonth": firstdatetime = getfirstdayofbeforemonthtime (). tostring (); lastdatetime = getlastdayofbeforemonthtime (). tostring ();
Break;
Default: firstdatetime = ""; lastdatetime = "";
Break;
}
}
}

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.