I can correct it a bit because according to ISO 8601 standard, As a consequence, if 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday , it is in week 52 or 53 of the previous year (there is no week 00). 28 December is always in the last week of its year.
Here, I adjust for the last week of the old year, if calculated according to the day of the old year eg 28-31/12 old year, it will be week 52, and calculated according to new year 1/1/new year will be week first
so I can tweak this a bit instead of you using the case for the 53rd week because my test shows 52 weeks for 365 days of the year.
int wk = nds<7? 1: nds / 7;
using System.Globalization;
Console.Clear(); // xóa mà hình dos
Console.BackgroundColor = ConsoleColor.Blue; //đổi màu nền dos
Console.ForegroundColor = ConsoleColor.White; // đổi màu chữ trong dos
Console.WriteLine("Hello, World!");
CultureInfo cul = CultureInfo.CurrentCulture;
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar cal = cul.Calendar;
string ngay1 ="2023-01-01", ngay2 = "2022-12-31";
DateTime day1 = DateTime.Parse(ngay1), day2= DateTime.Parse(ngay2);
DateTime day3 = new DateTime(2022, 1, 2), day4 = new DateTime(2022,1,2);
Console.WriteLine("so sánh chuỗi!:"+ (ngay1.Equals(ngay2)).ToString());
Console.WriteLine("tuan cua ngay :" + day1.ToString() + " là: " + cal.GetWeekOfYear(day1, dfi.CalendarWeekRule, dfi.FirstDayOfWeek));
Console.WriteLine("tuan cua ngay :"+day2.ToString()+ " là: " + cal.GetWeekOfYear(day2, dfi.CalendarWeekRule,dfi.FirstDayOfWeek) );
//
DateTime fromDate = new DateTime(2022, 12, 31);
DateTime startOfYear = fromDate.AddDays(-fromDate.Day + 1).AddMonths(-fromDate.Month + 1);
Console.WriteLine("startOfYear: " + startOfYear.ToString());
DateTime endOfYear = startOfYear.AddYears(1).AddDays(-1);
Console.WriteLine("endOfYear: " + endOfYear.ToString());
int[] iso8601Correction = { 7, 8, 9, 10, 4, 5, 6 };
Console.WriteLine("fromDate.Subtract(startOfYear).Days: " + fromDate.Subtract(startOfYear).Days.ToString());
Console.WriteLine("[(int)startOfYear.DayOfWeek]: " + startOfYear.DayOfWeek.ToString());
Console.WriteLine("iso8601Correction[(int)startOfYear.DayOfWeek]: " + iso8601Correction[(int)startOfYear.DayOfWeek].ToString());
int nds = fromDate.Subtract(startOfYear).Days + iso8601Correction[(int)startOfYear.DayOfWeek];
Console.WriteLine("nds: " + nds.ToString());
int wk = nds<7? 1: nds / 7;
DateTime NewDate = fromDate;
switch (wk)
{
case 0:
// Return Week Number of Previous Year in some cases (1/1/2016)
NewDate = startOfYear.AddDays(-1);
startOfYear = fromDate.AddDays(-NewDate.Day + 1).AddMonths(-NewDate.Month + 1);
endOfYear = startOfYear.AddYears(1).AddDays(-1);
nds = NewDate.Subtract(startOfYear).Days + iso8601Correction[(int)startOfYear.DayOfWeek];
wk = nds / 7;
break;
case 53:
// Adjust
if (endOfYear.DayOfWeek < DayOfWeek.Thursday)
{
wk = 1;
}
//else
// wk = wk;
break;
}
Console.WriteLine("tuan cua ngay :" + wk.ToString());
//
Console.Read();
according to the standards of ISO 8601 and System.Globalization; then it’s 1 week apart like 10/14/2022 according to System.Globalization; is the 42nd week because there are 53 weeks but according to ISO 8601 only 41 because there are only 52 weeks
Thank you so much good luck!
