tags:

views:

77

answers:

3

How can I code a calendar? I mean how can I determine the days of the month, is the 1st a monday, tuesday or what. how many days are there in the particular month etc.

I use mainly PHP & C#. But I am mainly looking for the logic behind

+1  A: 

Most languages (PHP and C# included) will provide utility functions or libraries which allow you to play with dates without having to get too involved in the gritty calculations.

PHP's date() and getdate() functions will tell you most details about a given date, including the day of week, day of month and day of year, provided you start with it in timestamp format.

If you want to find the details of the first of a given month, use the PHP mktime() function to get the time stamp for the date you want, then pass that into getdate() as above.

To find out how many days long a month is, you could either use a lookup table (ie Jan=31, etc), with a special case for Feb, or use the above method to get the 1st of the next month, and subtract 24 hours to get the last of the current month.

If you're using an up-to-date version of PHP (ie 5.3), there is a very useful new date class which supersedes most of the old date/time functionality, and provides a lot more besides. (it's also there in PHP 5.2, but not nearly as good as the 5.3 version).

Spudley
+1  A: 

Take a look at the DateTime type in the MSDN for C#.

You can determine the Weekday of a given day the following way:

var date = DateTime.Parse("16.10.2010");

Console.WriteLine(date.DayOfWeek);

If you are looking for the amount of days in a month, try this:

Console.WriteLine(DateTime.DaysInMonth(date.Year, date.Month));
Dave
+1  A: 

Take a look at the GregorianCalendar class in C#. This will tell you what you need to know regarding a date according to different date "rules".

This will help you find things like the week number of a date that the DateTime class won't manage for you.

Øyvind Bråthen