Click here to Skip to main content
15,915,611 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi.
i have year,month and week number of month and week day.
for example:
1994,jun,week number in month:4, monday.
how can i get date?
Posted

The solution is country dependent !
In Europe week start on Monday, in USA week start on Sunday.

This is not a programming problem, it is a mathematical problem.

You have to find the algorithm for your country because the week don't start the same day in every country. I suggest to use Wikipedia.
https://en.wikipedia.org/wiki/ISO_week_date[^]
https://en.wikipedia.org/wiki/Week[^]

Conventions in your date data may also play in the solution:
- how are you numbering the first week of a month when it is only 1 or 2 days ?
 
Share this answer
 
v3
Comments
PIEBALDconsult 29-Aug-15 18:58pm    
"in USA week start on Sunday"

No. There are International Standards that specify Monday. These apply to the U.S.
Patrice T 29-Aug-15 19:19pm    
Wikipedia disagree with you :)
https://en.wikipedia.org/wiki/Week
It is mostly maths, try this example to find the date for the Wednesday on 4th week of February 2016. Test it with other dates and parameter, say 5th week, it will cross over to March.
using System;

public class Program
{
	public static void Main()
	{
		DateTime dt = new DateTime(2016, 2, 1); // create the start date of the month and year
		DayOfWeek firstDayOfWeekofMonth = dt.DayOfWeek; // Find out the day of week for that date
		int myWeekNumInMonth = 4; // You want the 4th week, this may cross over to the following month!
		string myDayOfWeek = "Wednesday";  // You want Wednesday
		int myDayOfWeekInt = day2Int(myDayOfWeek);
		int diff = myDayOfWeekInt - (int)firstDayOfWeekofMonth;
		Console.WriteLine("The answer is {0:d}.", dt.AddDays(7 * (myWeekNumInMonth - 1) + diff));
	}

	public static int day2Int(string dayOfWeek)
	{
		switch (dayOfWeek)
		{
			case "Sunday":
				return 0;
			case "Monday":
				return 1;
			case "Tuesday":
				return 2;
			case "Wednesday":
				return 3;
			// Finish the rest yourself
			default:
				return -1; // Do error checking
		}
	}
}

Find out more:
1. DayOfWeek Enumeration[^]
2. DateTime.AddDays Method[^]
 
Share this answer
 
v2
Comments
rahman_jalayer 30-Aug-15 10:52am    
thanks a lot mr. Peter Leow.
it works great.
thank you.
Peter Leow 31-Aug-15 5:23am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900