Alex Rivera | Logout

DateTime string format last digit of year

Asked 2012-03-21T11:29:50.053
8

I wonder if there is a better way to get the last digit of a year from a datatime object

var date = DateTime.Now;
string year1 = date.ToString("y"); //NOT OK return Month year (eg March 2012)
char year2 = date.ToString("yy")[1]; //OK return last digit of the year (eg 2)
char year3 = date.ToString("yy").Last(); //OK same as above using linq

Anyone know if an already predifine format exist for retreiving the last digit of the year?

Thanks

Edit
Report

1 Answer

25

You can do that with simple Modulo math:

int digit = date.Year % 10;
answered 2012-03-21T11:32:10.100

Your Answer