Get current quarter in the year - CSharp Language Basics

CSharp examples for Language Basics:Date Time

Description

Get current quarter in the year

Demo Code

using System;//from ww  w  .  ja v  a2 s. c om
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
   static void Main(string[] args)
   {
      DateTime enteredDate = DateTime.Now;
      int enteredYear = enteredDate.Year;
      int enteredMonth = enteredDate.Month;
      int numberOfQuarter = (enteredMonth + 2) / 3;
      int monthOfQuarterStart = 3 * numberOfQuarter - 2;
      DateTime firstDayOfQuarter = new DateTime(enteredYear, monthOfQuarterStart, 1);
      DateTime lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);
      Console.WriteLine("Corresponding quarter: " + "number-" + numberOfQuarter + ", from " + firstDayOfQuarter.ToShortDateString() + " to " + lastDayOfQuarter.ToShortDateString());
   }
}

Result


Related Tutorials