Keith M. Dennis

Hi guys,

I have a mind boggling datetime problem I'd like some help with. Here's the scenario:

Given the week number of the current week of the year (calculated as (DateTime.Now.DayOfYear / 7) + 1) I need to obtain a range of week numbers that fall between:

  1. LastMonth i.e from the first to the last day of last month.
  2. LastQuarter i.e from the first day to the last day of the of the last quarter (6 months ago to 3 months ago).
  3. ThisMonth i.e from the first to the last day of this month.
  4. ThisQuarter i.e from the first day to the last day of the of this quarter (3 months ago to now).

Any pointers will be greatly appreciated.

Thanks.

Keith.


Re: Visual C# General DateTime Challenge

boban.s

Here is how:

public static DateTime FirstDayOfQuarter(DateTime date)

{

int intQuarterNum = (date.Month - 1) / 3 + 1;

return new DateTime(date.Year, 3 * intQuarterNum - 2, 1);

}

public static DateTime LastDayOfQuarter(DateTime date)

{

return FirstDayOfQuarter(date).AddMonths(3).AddDays(-1);

}

public static DateTime FirstDayOfMonth(DateTime date)

{

return date.AddDays(1 - date.Day);

}

public static DateTime LastDayOfMonth(DateTime date)

{

return FirstDayOfMonth(date).AddMonths(1).AddDays(-1);

}

public static int WeekNumber(DateTime date)

{

return date.DayOfYear/7 + 1;

}


Now if we test these functions:
Debug.WriteLine("This month " + WeekNumber(FirstDayOfMonth(DateTime.Today)) + "-" + WeekNumber(LastDayOfMonth(DateTime.Today)));

Debug.WriteLine("This quarter " + WeekNumber(FirstDayOfQuarter(DateTime.Today)) + "-" + WeekNumber(LastDayOfQuarter(DateTime.Today)));

Debug.WriteLine("Last month " + WeekNumber(FirstDayOfMonth(DateTime.Today.AddMonths(-1))) + "-" + WeekNumber(LastDayOfMonth(DateTime.Today.AddMonths(-1))));

Debug.WriteLine("Last quarter " + WeekNumber(FirstDayOfQuarter(DateTime.Today.AddMonths(-3))) + "-" + WeekNumber(LastDayOfQuarter(DateTime.Today.AddMonths(-3))));


This will be printed for current date 28 july 2007:

This month 27-31

This quarter 27-40

Last month 22-26

Last quarter 14-26






Re: Visual C# General DateTime Challenge

KJacquemin

First, don't get the current week that way. Use the built in functionality:

Calendar cal = CultureInfo.InvariantCulture.Calendar;

int startweek = cal.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);

The rest is just a mater of doing some DateTime calculations.

LastMonth

DateTime Workdate = DateTime.Now; // get todays date

Workdate.Day = 1; //Set the date to the start of the month

DateTime Enddate = Workdate.AddDays(-1); //Subtract 1 day to get the last day of last month

DateTime Startdate = Workdate.AddMonths(-1); //Subtract 1 month to get the first day of last month.

int startweek = cal.GetWeekOfYear(Startdate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);

int endweek = cal.GetWeekOfYear(Enddate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);

Use similar calculations for the other three spans you need.





Re: Visual C# General DateTime Challenge

OmegaMan

FYI: Be wary that actual business days may not fall on the calculated starts due to holidays or other issues such as weekends.





Re: Visual C# General DateTime Challenge

AlexBB

Omega hi,

Do you know of any function that will calculate trading days Or alternatively, give all the holidays that could be excluded For many years ahead.

I am aware of a slew of routines to calculate Easter and Passover but it is an area so unduly complex for me I would hate to spend a day checking them all out. I want something proven and simple.

Thanks.






Re: Visual C# General DateTime Challenge

OmegaMan

AlexBB wrote:

Omega hi,

Do you know of any function that will calculate trading days Or alternatively, give all the holidays that could be excluded For many years ahead.

I am aware of a slew of routines to calculate Easter and Passover but it is an area so unduly complex for me I would hate to spend a day checking them all out. I want something proven and simple.

Thanks.



I wish I knew of such a function or service....

I had to contact someone at the NYSE to get the holiday schedule for the current year and next. From that I extracted each date's specific year count number and stored that in an xml structure. When my program(s) needed to determine if they could pull data for a specific day/week/month start day, it had to be a business day, they would consult the Xml and determine if the current date fell on a holiday/non-trading day.

Unfortunately, as I found, trading days/ holidays / Presidential funerals are flux and cannot be predicted. Another fly in the ointment is that one market could be closed but another one, either within the US, or outside is actively trading.





Re: Visual C# General DateTime Challenge

AlexBB

OmegaMan wrote:
AlexBB wrote:

Omega hi,

Do you know of any function that will calculate trading days Or alternatively, give all the holidays that could be excluded For many years ahead.

I am aware of a slew of routines to calculate Easter and Passover but it is an area so unduly complex for me I would hate to spend a day checking them all out. I want something proven and simple.

Thanks.



I wish I knew of such a function or service....

I had to contact someone at the NYSE to get the holiday schedule for the current year and next. From that I extracted each date's specific year count number and stored that in an xml structure. When my program(s) needed to determine if they could pull data for a specific day/week/month start day, it had to be a business day, they would consult the Xml and determine if the current date fell on a holiday/non-trading day.

Unfortunately, as I found, trading days/ holidays / Presidential funerals are flux and cannot be predicted. Another fly in the ointment is that one market could be closed but another one, either within the US, or outside is actively trading.

Thanks,

Actually, I think QT website, if I am not mistaken, has a schedule too at least partial. I haven't had time to focus on it close enough but it is coming.