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.
Related
I am displaying a list of items in a data grid binded to dt_pdc. The DueDate column shows the date of the cheque, as day, month and year. When i'm sorting using descending, it does the following:
Date List & Order:
1---4/18/2020
2---4/2/2020
3---4/22/2020
When the day's first digit is less than another day's first digit, it is being sorted first, in the example, 18 april is coming before 2 april, however 22 april comes after 2 april.
Is there anything that i can fix in the sorting view, or do i have to write it in the DB as 02 instead of 2.
dt_pdc.Columns.Add("ID");
dt_pdc.Columns.Add("ChequeNumber");
dt_pdc.Columns.Add("DueDate");
dt_pdc.Columns.Add("Amount");
dt_pdc.Merge(Database.Accounts.Cheques.getPDCChequesSearch(dt_pdc));
dt_pdc.DefaultView.Sort = "DueDate ASC";
or do i have to write it in the DB as 02
no, that's just compounding the error (wait until you get an urgent support call on new year's day if you don't believe me); basically: stop storing dates as strings; store them as dates - i.e. DateTime; then everything will work correctly. If you absolutely must use string for some reason (and it would need to be a good reason), consider using ISO8601 format, i.e. store it as "2020-04-02"; this is then sortable naturally as a string, plus it is unambiguous (there is no question as to whether this is the 2nd of April or the 4th of February).
I am trying to get a label to display the Julian date in a specific format. The last two digits of the year then the day in the year, so for example January 1 2021 would be 210001. I am having difficulty getting it to display both of these values attached and making the day slot have 3 values instead of 2.
This is what I have.
This just gives the day of the year but still not as a 3 digit day so it shows 1 as 1 instead of 001 which is my goal
Any help would be appreciated! :)
Unfortunately the date time formats do not include anything for day of the year so you'll have to create this yourself. You can format a number to have leading zeros using the D format where you specify the length you want. You can however use the date formats to get the last two digits of the year. So the following should give you the desired formatted string for a date.
public string ToYYJJJ(DateTime date)
{
return date.ToString("yy") + date.DayOfYear.ToString("D3");
}
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
Can anyone explain to me why this unit test is failing?
I scoured through MSDN expecting to find an explanation, for example I was expecting to find something like, "the starting day is not inclusive" etc. but I found no such statement. Therefore I am confused as to why this seems to be off by one day.
The following will result in Sept 29th. I am expecting Sept 30th.
[Test]
public void AddDaysBug_OffByOne()
{
DateTime end = new DateTime(2018,10,3);
DateTime fourDaysEarlier = end.AddDays(-4);
// this fails. 29!=30
Assert.AreEqual(fourDaysEarlier.Day,30, "four days prior to October 3 is Sept 30");
}
Lets take these days one at a time for illustration...
3rd - 1 = 2nd
2nd - 1 = 1st
1st - 1 = 30th
30th - 1 = 29th
There is no zero day in months as with "normal" numbers.
Setp: 28, 29, 30
Oct: 1, 2, 3
So: (3 Oct - 4 day) is equal to 29
Sept 29th seems to be correct answer.
It's easy to see if you subtracting one day at a time.
-1 day, 10/02/2018
-2 day, 10/01/2018
-3 day, 09/30/2018
-4 day, 09/29/2018
I'm trying to calculate the age based on the DOB.
Int32 DOB = 19900427;
Int32 current = 20140111;
Int32 result = current - dob;
Now i just need to display the starting 2 digit of the result in a text box. Could you please help me with this?
Don't do it that way. Just don't. You can't get a useful age representation by subtracting one value from another - you'll find that the difference between two people who were born a day apart can differ massively based on exactly when those dates are.
For example, consider three people with birth dates of:
A: December 30th 2013 - 20131230
B: December 31st 2013 - 20131231
C: January 1st 2014 - 20140101
That gives a difference between the ages of A and B of 1, but a difference between the ages of B and C of 8870. It's surely not good for you.
Use DateTime to represent dates - or preferably, use LocalDate from my Noda Time library. Then you can determine the difference between the dates however you want - potentially just in a number of days, for example.