【问题标题】:How can I format "MMyy" to "MM/yyyy" in C# [closed]如何在 C# 中将“MMyy”格式化为“MM/yyyy”[关闭]
【发布时间】:2020-03-24 21:14:29
【问题描述】:

我有一个以“MMyy”格式存储在字符串中的日期。我想成为一个字符串,但将其转换为 "MM/yyyy"

我尝试了以下方法,但它不起作用

public static string formatDate(string inputDate)
{
   DateTime.TryParseExact(inputDate, "MMyyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out 
      DateTime resultDate);
   return resultDate.ToString("MM/yyyy");
}

【问题讨论】:

  • “但它不起作用”不是报告错误的最佳做法。另外,如果日期存储为“MMyy”,你会尝试用“MMyyyy”来解析它吗?
  • 享受处理千年虫问题...

标签: c# .net datetime


【解决方案1】:

看起来格式不正确。您可以执行以下操作:

public static string formatDate(string inputDate)
{
   DateTime.TryParseExact(inputDate, "MMyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out 
      DateTime resultDate);
   return resultDate.ToString("MM/yyyy");
}

【讨论】:

    猜你喜欢
    • 2021-01-09
    • 2019-05-15
    • 2021-08-01
    • 2017-04-16
    • 2020-05-30
    • 2018-05-01
    • 1970-01-01
    • 2013-11-01
    • 2020-08-28
    相关资源
    最近更新 更多