今年的DateTime字符串格式最后一个数字 [英] DateTime string format last digit of year

查看:292
本文介绍了今年的DateTime字符串格式最后一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有更好的方法来从DATATIME对象

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



任何人都知道,如果一个已经predifine格式用于检索的最后一位数字存在今年?

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

感谢

推荐答案

您可以做到这一点简单的数学模:

You can do that with simple Modulo math:

int digit = date.Year % 10;

这篇关于今年的DateTime字符串格式最后一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆