I'm getting System.ArgumentOutOfRangeException error message when I'm trying to substring a year off of a string which is in DateTime format ("mm/dd/yyyy hh:mm:ss xx") and assign it to a List
/ ArrayList
.
for(int i = 0; i < elementCounter; i++)
{
String stringDate = Convert.ToString(DatumPocetka[i]);
DPGodina[i] = stringDate.Substring(stringDate.Length - 16, 4);
}
DatumPocetka is an ArrayList with string type values which are in DateTime format I mentioned earlier.
I have to calculate the starting character from the back of the string though. I don't know the index where the year starts when I'm calculating it from the beginning because days and months can be single and double digits. It's easier to calculate it from the back because the time part of the string stays static. ("mm/dd/yyyy hh:mm:ss xx") this part of the string always has 16 characters, so that's why I'm subtracting the length by 16.
Even Visual Studio says that stringDate.Length is equal to 21, but I'm still getting the ArgumentOutOfRangeException on the DPGodina[i] = stringDate.Substring(stringDate.Length - 16, 4);
line .
I have tried doing this with List<DateTime> using name.Year.ToString(); but that did not work either.