我最近在一个大型系统上进行了代码评论,该系统在世界各地都有用户。
我注意到一些开发人员使用以下代码显示日期:
userDOB.ToString("d")
和其他人正在使用以下方式:
userDOB.ToShortDateString()
这两条线应输出什么区别?如果不是这样,我将推动具有一致性并使用以下格式之一,我个人更喜欢ToshortDateString(),因为它更可读(除非有人有更好的理由使用ToString(" D"))。
如果您看到两种方法
public string ToShortDateString()
{
return DateTimeFormat.Format(this, "d", DateTimeFormatInfo.CurrentInfo);
}
和
public string ToString(string format)
{
return DateTimeFormat.Format(this, format, DateTimeFormatInfo.CurrentInfo);
}
您可以看到自我的区别,在ToString
中,您必须在情况下定义格式"d"
,而在ToShortDateString
中,格式是预定的,但它们都指向相同的方法DateTimeFormat.Format