C # and SQL computing time: Monday, Sunday, beginning of month, end of month, beginning of the quarter, end of the quarter...

Source: Internet
Author: User
Public void GetDate (DateTime thisDate, out DateTime endWeek, out DateTime endMonth, out DateTime endQuarter, out DateTime endYear) {// DateTime dt = Convert. toDateTime (thisDate. toString ("yyyy-MM-dd"); // DateTime startWeek = dt. addDays (1-Convert. toInt32 (dt. dayOfWeek. toString ("d"); // Monday of the week // endWeek = startWeek. addDays (7 ). addSeconds (-1); // this week's Sunday // DateTime beginMonth = dt. addDays (1-dt. day); // the beginning of this month // endMonth = beginMonth. addMonths (1 ). addSeconds (-1); // End of the month // DateTime startQuarter = dt. addMonths (0-(dt. month-1) % 3 ). addDays (1-dt. day); // The beginning of this quarter // endQuarter = startQuarter. addMonths (3 ). addSeconds (-1); // end of this quarter // DateTime startYear = new DateTime (dt. year, 1, 1); // early next Year // endYear = new DateTime (dt. year + 1, 1, 1 ). addSeconds (-1); // DateTime dt = Convert at the end of this year. toDateTime (thisDate. toString ("yyyy-MM-dd"); DateTime startWeek; if (Convert. toInt32 (dt. dayOfWeek. toString ("d") = 0) {startWeek = dt. addDays (-6);} else {startWeek = dt. addDays (1-Convert. toInt32 (dt. dayOfWeek. toString ("d"); // Monday of the week} // DateTime startWeek = dt. addDays (1-Convert. toInt32 (dt. dayOfWeek. toString ("d"); // This Week, Monday endWeek = startWeek. addDays (7 ). addSeconds (-1); // DateTime beginMonth = dt on Sunday of this week. addDays (1-dt. day); // endMonth = beginMonth at the beginning of this month. addMonths (1 ). addSeconds (-1); // DateTime startQuarter = dt at the end of the month. addMonths (0-(dt. month-1) % 3 ). addDays (1-dt. day); // endQuarter = startQuarter at the beginning of this quarter. addMonths (3 ). addSeconds (-1); // DateTime startYear = new DateTime (dt. year, 1, 1); // endYear = new DateTime (dt. year + 1, 1, 1 ). addSeconds (-1); // End Of The Year}

1. If the input time is Sunday, the next week will be regarded as the cost of Monday. there will be problems with the weekly calculation.

========================================================== ========================================================== ==========

2. The following describes how to calculate related time parameters in SQL.

DECLARE @ MondayTime varchar (10) DECLARE @ SundayTime varchar (10) DEClARE @ ThisDate datetimeDEClARE @ EndWeekDate datetime -- weekend time Declare @ EndMonth datetime -- end time DECLARE @ EndQuarter datetime -- end time set @ ThisDate = dateadd (day,-1, getdate ()) IF (datepart (weekday, @ ThisDate)-2 <0) begin set @ MondayTime = convert (varchar (10), dateadd (dd,-1, @ ThisDate) -(datepart (weekday, dateadd (dd,-1, @ ThisDate)-2), 120) SET @ SundayTime = convert (varchar (10), dateadd (dd, -1, @ ThisDate) + (8-datepart (weekday, dateadd (dd,-1, @ ThisDate), 120) endelse begin set @ MondayTime = convert (varchar (10 ), @ ThisDate-(datepart (weekday, @ ThisDate)-2), 120) SET @ SundayTime = convert (varchar (10), @ ThisDate + (8-datepart (weekday, @ ThisDate), 120) END set @ EndWeekDate = convert (datetime, @ SundayTime) set @ EndWeekDate = dateadd (second, 59, dateadd (minute, 59, dateadd (hour, 23, @ EndWeekDate) -- select @ EndWeekDate set @ EndMonth = dateadd (second, 59, dateadd (minute, 59, dateadd (hour, 23, dateadd (month, 1 + datediff (month, 0, @ ThisDate), 0)-1) -- select @ EndMonth as endMonth declare @ Num int; declare @ spanMonth int; set @ Num = datepart (MM, @ EndMonth) if (@ Num <= 3) set @ spanMonth = 3-@ Num else if (@ Num> 3 and @ Num <= 6) set @ spanMonth = 6-@ Num else if (@ Num> 6 and @ Num <= 9) set @ spanMonth = 9-@ Num else if (@ Num> 9 and @ Num <= 12) set @ spanMonth = 12-@ Num set @ EndQuarter = dateadd (month, @ spanMonth, @ EndMonth) -- select @ EndQuarter as endQuarter

 

 

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.