Link to home
Start Free TrialLog in
Avatar of Andy Green
Andy GreenFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to calculate which day of month a given day is.

Hi All

Not a very good title, but what I need is to calculate out the interval and week day of any given date.

ie today is the 3rd Tuesday.

Need to do this in ASP.net V4

Andy
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good spot :)
same idea but different
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication34
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime today = DateTime.Today;

            DayOfWeek day = today.DayOfWeek;
            int interval = (today.Day - 1) / 7 + 1;
            string suffix;
            switch (interval)
                {
                case 1:
                    suffix = "th";
                    break;
                case 2:
                    suffix = "st";
                    break;
                case 3:
                    suffix = "rd";
                    break;
                default:
                    suffix = "th";
                    break;
                }
            Console.WriteLine("Today is the {0}{1} {2} of the month", interval,suffix,Enum.GetName(typeof(DayOfWeek), day));
                 Console.ReadLine();                                         
        }
    }
}

Open in new window

I did think about putting a switch in there, but it didn't seem worth it for 3 values.  I also toyed with an extension method, which for repeated use would be handy, but a bit overkill for a quick example.
I like those answers.  Here's a mindless approach that simply iterates backwards to the 1st of the month and counts the number of days that match the one passed in:
        public string DayInterval(DateTime dt)
        {
            int count = 1;
            DayOfWeek day = dt.DayOfWeek;
            while(dt.Day != 1)
            {
                dt = dt.AddDays(-1);
                if (dt.DayOfWeek == day)
                {
                    count++;
                }
            }
            string[] suffix = { "", "st", "nd", "rd", "th", "th" };
            return count.ToString() + suffix[count] + " " + day.ToString();
        }

Open in new window

Avatar of louisfr
louisfr

Mindless indeed. I think I once used LINQ for this kind of computation, as a proof of concept.
Here's one with ~slightly~ more thought put into it...but still without doing a straight calculation.  More coffee is required before I can do more "complex" math.  ;)
        private string[] suffix = {"st", "nd", "rd", "th", "th" };

        public string DayInterval(DateTime dt)
        {
            int count = 1;
            while(dt.Day > 7)
            {
                count++;
                dt = dt.AddDays(-7);
            }
            return count.ToString() + suffix[count - 1] + " " + dt.DayOfWeek.ToString();
        }

Open in new window

Is this a challenge?
public string DayInterval(DateTime dt)
{
    var days = from d in Enumerable.Range(1, dt.Day)
            where new DateTime(dt.Year, dt.Month, d).DayOfWeek == dt.DayOfWeek
            select d;
    return days.Count() + suffix[count - 1] + " " + dt.DayOfWeek;
}

Open in new window

I Love It!  Some great answers in here now (and some not so great; *mine).  Does this mean we have to go back to "real" work now?
Avatar of Andy Green

ASKER

Thanks guys, Carl I have integrates yours into my code and it fails sometimes, ie it says that the 23 feb 2015 is the 3rd Monday, where it is the 4th.

I'll continue with the other solutions.

Andy
Actually ignore that - I had removed the +1 for debugging. I'll post some other discrepancies in a bit.

Andy
Not detecting Sundays at all
17 Feb is 3rd Tues with is correct but the 18 is the 4th Wed 19 is 4th Thurs 20 is 4th Fri which are all wrong.

Here is my code have I used it correctly?

                Dim today As DateTime = CType(rtpSlotStart.SelectedDate, DateTime)
                Dim day As DayOfWeek = today.DayOfWeek - 1
                Dim interval As Integer = ((today.Day) / 7) + 1

                ddlMonthDayInterval.SelectedValue = interval
                ddlMonthDay.SelectedValue = Weekday(rtpSlotStart.SelectedDate) - 1 (used as my days start at 0 not 1)

ANdy
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The code Carl provided uses integer divisions.
Your VB code uses floating-point divisions and rounds the result.
Use the integer division operator (and subtract 1 because days start at 1):
Dim interval As Integer = (today.Day - 1) \ 7 + 1

Open in new window

Thank guys, sorry I give point all round. to be fair I have used Carls first solution.

Andy