从日期的日期部分中删除前导0的最简洁方法是什么?

B. *_*non -4 c# datetime text-parsing date-math date-manipulation

我有这个代码,在关于框中显示一些构建信息:

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();
}
Run Code Online (Sandbox Code Playgroud)

今天看起来像这样:

在此输入图像描述

"问题"是屏幕房地产有限,而"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;
Run Code Online (Sandbox Code Playgroud)

(我使用"+ ="因为lblLastBuilt在设计时设置为"Last built".

那么:是否有一种不那么野蛮的方式来阻止前导0出现在日期字符串的"日期"部分?

Hab*_*bib 6

使用自定义格式. (MMMM d, yyyy)

String lds = computedDate.ToString("MMMM d, yyyy", CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)

single d会给你一个或两位数的日期部分.如果日期部分低于10,那么您将只获得一个数字而不是领先0,对于其他人,您将得到两个数字.

请参阅:自定义日期和时间格式字符串

我更喜欢"2015年2月4日"

编辑:我错过了星期几的部分,我不确定你是否需要,但如果你需要,你可以添加dddd自定义格式,如:

dddd, MMMM d, yyyy
Run Code Online (Sandbox Code Playgroud)