我有这段代码在 About 框中显示了一些构建信息:
private void frmAbout_Load(object sender, EventArgs e)
{
Version versionInfo =
Assembly.GetExecutingAssembly().GetName().Version;
lblVersion.Text = String.Format("Version {0}.{1}",
versionInfo.Major.ToString(), versionInfo.Minor.ToString());
String versionStr = String.Format("{0}.{1}.{2}.{3}",
versionInfo.Major.ToString(), versionInfo.Minor.ToString(),
versionInfo.Build.ToString(), versionInfo.Revision.ToString());
lblBuild.Text = String.Format("Build {0}", versionStr);
DateTime startDate = new DateTime(2000, 1, 1); // The date from
whence the Build number is incremented (each day, not each
build; see http://stackoverflow.com/questions/27557023/how-can-
i-get-the-build-number-of-a-visual-studio-project-to-increment)
int diffDays = versionInfo.Build;
DateTime computedDate = startDate.AddDays(diffDays);
lblLastBuilt.Text += computedDate.ToLongDateString();
}
今天看起来像这样:
“问题”是屏幕空间有限,“2015 年 2 月 4 日”之类的日期对我来说看起来很怪异(我更喜欢“2015 年 2 月 4 日”)。
我可以像这样粗暴地暴力破解从 ToLongDateString() 返回的字符串:
String lds = computedDate.ToLongDateString();
lds = // find leading 0 in date and strip it out or replace it with an empty string
lblLastBuilt += lds;
(我使用“+=”,因为 lblLastBuilt 在设计时设置为“最后构建”。
那么:是否有一种不那么暴力的方法来防止前导 0 出现在日期字符串的“日期”部分中?