Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime string format last digit of year

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

like image 671
Guillaume Avatar asked Mar 21 '12 11:03

Guillaume


1 Answers

You can do that with simple Modulo math:

int digit = date.Year % 10;
like image 158
Roy Dictus Avatar answered Oct 02 '22 11:10

Roy Dictus