In MS Excel with US English culture, while checking the Day of the week for the Date "1/1/1900", it returns SUNDAY, whereas in DayofWeek property of DateTime, it returns MONDAY. Also, checked with the Calender, it shows DayOFWeek for the Date "1/1/1900" is Monday. Hence, can any one please explain the behavior of DayOfWeek in Excel?
January 1, 1990 was a Monday (https://en.wikipedia.org/wiki/January_1900#January_1,1900(Monday))
The "WEEKDAY" Excel function returns a number from 1 (Sunday) to 7 (Saturday) representing the day of the week of a date.
System.DayOfWeek is an enumeration going from 0 (Sunday) to 6 (Saturday) (https://learn.microsoft.com/en-us/dotnet/api/system.datetime.dayofweek?view=netframework-4.8)
You pointed out a well known problem. As explained here, https://support.microsoft.com/en-us/help/214326/excel-incorrectly-assumes-that-the-year-1900-is-a-leap-year
"The WEEKDAY function returns incorrect values for dates before March 1, 1900. Because most users do not use dates before March 1, 1900, this problem is rare."
See also this answer: https://superuser.com/a/481499
Related
This question already has answers here:
Get the correct week number of a given date
(20 answers)
Closed 4 years ago.
I am trying to get the week number from a date time and in my case, the first day of the week is Monday and I want to follow the FirstFourDays convention.
To check the results, I am checking this webpage:
https://espanol.epochconverter.com/semanas/2020
To get the week number, I using the method:
System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear();
So I am trying to get the week number of the date 2019-12-29, so I use this code:
System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(new DateTime(2019, 12, 29), System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
The result is week 52. It is correct.
Now I am trying to get the week number of the 2019-12-30, the week number that I get is 53, it is wrong, because 2019 has only 52 weeks. In fact, 2019-12-30 belongs to the same week than 2020-01-01, that it is week 1, that is correct, so I don't understand why I can get two different results for the same date.
How I could get the correct result always? Or how would it be the correct way to get the week number of any date?
There's a blog article explaining this behavior and proposing a solution.
The issue:
Several people have noticed that Calendar.GetWeekOfYear() is almost like the ISO 8601 week when passed CalendarWeekRule.FirstFourDayWeek and DayOfWeek.Monday, however it is a little bit different. Specifically ISO 8601 always has 7 day weeks. If the first partial week of a year doesn't contain Thursday, then it is counted as the last week of the previous year. Likewise, if the last week of the previous year doesn't contain Thursday then its treated like the first week of the next year. GetWeekOfYear() has the first behavior, but not the second.
The proposed solution would be this:
A simple workaround to consistently get the ISO 8601 week is to realize that consecutive days Monday through Sunday in ISO 8601 weeks all have the same week #. So Monday has the same week # as Thursday. Since Thursday is the critical day for determining when the week starts each year my solution is to add 3 days if the day is Monday, Tuesday or Wednesday. The adjusted days are still in the same week, and use values that GetWeekOfYear and ISO 8601 agree on.
// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetIso8601WeekOfYear(DateTime time)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
DayOfWeek day = cal.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return cal.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
All credits for this go to Shawn Steele.
When saving a standard DateTime from .NET into a SQLite table I get values like this saved:
636183611669074150
What exactly is this format? I am doing some bug fixing and would like to know the exact date held by the number without running my app.
The DateTime.Ticks documentation says:
The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 (0:00:00 UTC on January 1, 0001, in the Gregorian calendar).
Is there a possibility to get the full date out of the year and the dayOfYear value?
For example: today is Thursday, 19th of february 2015.
The dayOfYear-Value of today is 50.
If I have the value 75 and the year 2010, how am I able to get the matching date?
It could be displayed in a textBox, dateTimePicker, whatever.
But you only have the information year & dayOfYear.
Thanks
You can use the following code:
DateTime day = New DateTime(2010, 1, 1).AddDays(75 - 1);
First get the first day of the year, then add necessary day count minus one (you are already on the first date of the year) days to the first day and you are there.
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; }
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 :
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
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
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.)
I accidentally passed 0 into DateTimeFormatInfo's GetMonthName method:
DateTimeFormatInfo info = new DateTimeFormatInfo();
var monthName = info.GetMonthName(0);
and got a System.ArgumentOutOfRangeException with this error message: Valid values are between 1 and 13, inclusive.
Passing in 1 through to 12 return "January" through to "December" but passing in 13 returns an empty string.
I can see why month numbers are not zero indexed, but what's month 13 for?
It's because calendar objects can accomodate 13 months (to handle calendars based on lunar months), see MSDN:
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.getmonthname.aspx
Calendar objects can accommodate calendars with 13 months. For 12-month calendars, the empty string is always returned as the name of the 13th month.
According to MSDN
Calendar objects can accommodate
calendars with 13 months. For 12-month
calendars, the empty string is always
returned as the name of the 13th
month.
I guess it is used to determine the leap day in the julian calendar ( http://en.wikipedia.org/wiki/Julian_calendar ). As most of use use Gregorian calender just do not worry.