如何从周数和周数获取日期? [英] how to get date from week number of month and week day?

查看:316
本文介绍了如何从周数和周数获取日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

i有月,月和周的月和星期数。

例如:

1994,jun,week月份数:4,星期一。

我怎么得到约会?

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?

推荐答案

解决方案依赖于国家!

周一欧洲周开始,周日美国周开始。



这不是一个编程问题,这是一个数学问题。



您必须找到您所在国家/地区的算法,因为每周都不会在同一天开始。我建议使用维基百科

https://en.wikipedia.org / wiki / ISO_week_date [ ^ ]

https://en.wikipedia.org/wiki/Week [ ^ ]



日期数据中的约定也可以在解决方案中发挥作用:

- 如果只有1天或2天,你如何编号一个月的第一周?
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 ?


这主要是数学,试试这个例子来查找2016年2月第4周的星期三的日期。用其他日期和参数测试,比如说第5周,它会越过3月。

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
		}
	}
}



了解更多:

1. < a href =https://msdn.microsoft.com/en-us/library/system.dayofweek.aspx> DayOfWeek枚举 [ ^ ]

2. DateTime.AddDays方法 [ ^ ]


这篇关于如何从周数和周数获取日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆