【问题标题】:How do I change the format of the date outputted from monthCalendar如何更改从 monthCalendar 输出的日期格式
【发布时间】:2020-02-11 17:07:23
【问题描述】:

我现在的代码

monthCalendar1.MaxSelectionCount = 1;
String date = monthCalendar1.SelectionRange.Start.ToString();
date = date.Substring(0, 10);
DateLabel.Text = "Date Selected : " + date;

当它输出选择的日期时,它输出为 2/9/2020。如何更改它以使其输出为 02/09/2020,以便所有日期的字符数相同?

【问题讨论】:

标签: c#


【解决方案1】:

monthCalendar1.SelectionRange.StartDateTime - 因此在将其转换为字符串时指定所需的格式。例如,你可以写:

DateTime start = monthCalendar1.SelectionRange.Start;
string formattedStart = start.ToString("MM/dd/yyyy");

请注意,这是一种以美国为中心的日期格式,而您当前的代码仅使用当前文化的默认格式模式。如果您想要一个明确的、固定宽度的日期模式,我建议您改用 ISO-8601:

DateTime start = monthCalendar1.SelectionRange.Start;
string formattedStart = start.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2018-10-25
    相关资源
    最近更新 更多