获取给定日期的周号c# [英] Get week number for the given date c#

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

问题描述

我已经尝试搜索一个解决方案,为日期值提供正确的周数。

link1 link2 link3 _
按照上述链接中的方法,但是截止到2012年12月30日,我的周数为53.但是下降到2015年的第1周。

我尝试以下方法来获取特定日期的年份周数。

I have tried searching for a solution which gives the correct week number for the date value.
link1, link2,link3
Followed the methods in the above links, but for the date 30/12/2014, I get the week number as 53. but it falls as 1st week of 2015 year.
I tried the below methods to get the week number of the year for the specific date.

private int GetWeekNumberOfTheYear() {
var currentCulture = CultureInfo.CurrentCulture;
// option 1 
var weekNo = currentCulture.Calendar.GetWeekOfYear(DateTime.Now,currentCulture.DateTimeFormat.CalendarWeekRule, currentCulture.DateTimeFormat.FirstDayOfWeek);
// option 2 
var weekNo = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
return weekNo; }

上面的方法是否正确返回53作为周数或应该是1?

上述代码中有错误。请提出建议
编辑:

Is the method above is correct to return 53 as week number or it should be 1 ?
Is there any mistake in the above code. Suggestions please. EDIT :

发现许多搜索指定,2014年12月29日至2015年1月4日为2015年的第一周。混乱是本周必须被视为第53周或第1周。

Found many searches specified, Dec 29th 2014 to 4th Jan 2015 as 1st week of year 2015.
So my confusion is the present week must be taken as 53rd Week or 1st Week.

http://week-number.net/calendar-with-week-numbers-2014.html

http://www.epochconverter.com/date-and-time/weeknumbers-by -year.php

推荐答案

如果您正在寻找ISO-8601周的周您可以使用我的 Noda Time 项目:

If you're looking for the ISO-8601 week-of-week-year, you could use my Noda Time project:

var date = new LocalDate(2014, 12, 30);
var week = date.WeekOfWeekYear; // 1
var weekYear = date.WeekYear; // 2015

您可以获取 LocalDate DateTime 通过 LocalDateTime ,但最好是使用Noda时间通过您的项目尽可能广泛。 (这就是我希望的方式,你会得到最大的好处。)

You can get a LocalDate from a DateTime via a LocalDateTime, but ideally you'd use the Noda Time times as widely as possible through your project. (That's the way I'd hope you'd get the maximum benefit, anyway.)

这篇关于获取给定日期的周号c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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