hi in my program the users need to update his record every week. every week there will be a different food that will be given to him. i have a table where his info are saved, column week determine what week it is?(1 month = 4 weeks) i want to increment the value in week column to determine if its his 1st,2nd,3rd or 4rth week? if he updates his record on the 4rth week, column week will be 1 again.
example....
date___|week
august 1 | 1
august 8 | 2
august 16 | 3
august 24 | 4
august 31 | 1
sept 7 | 2

this is how i wanted to happen but dont know how... thanks...

Edited 5 Years Ago by kapojian: n/a

whats with the "day".. i think im goin for a hard code.. lols

Perhaps this will help you on the way, it workes for the month of august, as you pointed out.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int w;
      
            for (int d = 0; d < 31; d++)
            {
                w = (d / 7) % 4 + 1;
                Console.WriteLine("Day={0}   week={1}", d + 1, w);
            }
            Console.ReadKey();
        }
    }
}

To get a week number of the month you can do:

//in your method:
DateTime time = DateTime.Now;
int weekNumber = GetWeekOfMonth(time); //calling a method

        public static int GetWeekOfMonth(DateTime date)
        {
            DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);

            while (date.Date.AddDays(1).DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
                date = date.AddDays(1);

            return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays / 7f) + 1;
        }
Comments
Nice addition!

Just have in mind that a 31 days month starting on Saturday can have until 6 weeks.

Lets see a July Month assuming the Sunday is the first day of the week:
Saturday 1, Week 1
Monday 3, Week 2
Monday 10, Week 3
Monday 17, Week 4
Monday 24, Week 5
Monday 31, Week 6

An approach can be: Calculate the Week Of Year for the given date, calculate the Week OfYear for the first of the given month, and return the difference between them + 1.

To obtain the calculation of the week of year for a given date, the function must be aware of the locale date properties (see here for calendar info). I will suggest the following code:

public static int GetYearWeekNumber(DateTime dtPassed)
		{
			CultureInfo ciCurr = CultureInfo.CurrentCulture;
			int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
			return weekNum;
		}

Have in mind that this will work according to the rules to determine the first week of the year and first day of week for this culture (some use Sunday, some use Monday)

Having this function, the rest of calculation can be the following:

private static int WeekOfMonth(DateTime dateToTest)
		{
			var weekOfYearDateToTest = GetYearWeekNumber(dateToTest);
			var weekOfYearFirstOfMonth = GetYearWeekNumber(new DateTime(dateToTest.Year, dateToTest.Month, 1));
			return weekOfYearDateToTest - weekOfYearFirstOfMonth + 1;
		}

Hope this helps

This article has been dead for over six months. Start a new discussion instead.