将任何字符串格式化为字符串“yyyy / MM / dd”

时间:2012-03-21 14:48:25

标签: c# string formatting

我有许多字符串,如" 20120117"和" 20120321"。我需要使用以下格式将其转换为新字符串:" 2012/01/17"和" 2012/03/21"。那么,有办法做到这一点吗?

我试试:

string dateString = string.format("{0:d", "20120321");

string dateString = string.format("{0:yyyy/MM/dd", "20120321"); 

string dateString = int.Parse("20120321").ToString("yyyy/MM/dd");

我所有的情况都达不到我的目标。 = /

所以,我可以这样做吗?

OBS:有一种方法可以在不解析日期时间的情况下做到这一点吗?

8 个答案:

答案 0 :(得分:6)

DateTime.ParseExact("20120321", "yyyyMMdd", CultureInfo.CurrentCulture).ToString("yyyy/MM/dd")

您可以解析字符串,但此方法为您提供验证,无需任何额外代码。想象一下收到“20120230”,“20110229”或任何其他无效日期。

答案 1 :(得分:5)

您必须先在DateTime个对象中解析这些值。

示例:

DateTime dt = DateTime.ParseExact("20120321", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
var result = dt.ToString("yyyy/MM/dd");

在对其他答案发表评论后进行修改: 如果你不喜欢解析,因为它可能会引发异常,你可以随时使用TryParse,如下所示:

DateTime dt;
bool success = DateTime.TryParseExact("20120321", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
if (success)
{
    var result = dt.ToString("yyyy/MM/dd");
}

编辑2:使用多种格式的TryParseExact

DateTime dt;
string[] formats = { "yyyyMMdd", "yyyy" };

bool success = DateTime.TryParseExact("20120321", formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
if (success)
{
    var result = dt.ToString("yyyy/MM/dd");
    Console.WriteLine(result);
}

使用“20120321”作为输入值时会生成2012/03/21,使用2012/01/01作为输入值时会生成2012

答案 2 :(得分:4)

来自您的评论:

  

有一种方法可以在不解析日期时间的情况下做到这一点吗?

是的,绝对的。如果确实希望通过您的系统传播错误数据而不是突出显示它是不正确的,那么您肯定可以使用:

// Make sure we'll always be able to get a result whatever the input.
string paddedInput = input + "????????";
string mightBeBadWhoKnows = string.Format("{0}/{1}/{2}",
    paddedInput.Substring(0, 4), // The year, if we're lucky
    paddedInput.Substring(4, 2), // The month, if we're lucky
    paddedInput.Substring(6, 2)); // The day, if we're lucky

但为什么不会你想发现坏数据呢?

你绝对应该解析数据。如果您希望在收到采取适当措施的错误数据后继续,请使用DateTime.TryParseExact。如果您对抛出异常感到满意,请使用DateTime.ParseExact。我建议使用不变文化进行解析和格式化,除非你真的想要一个文化敏感的输出。

答案 3 :(得分:3)

使用DateTime.ParseExact转换为DateTime,然后在该实例上使用ToString进行格式化。

DateTime.ParseExact(dateString, "yyyyMMdd").ToString("yyyy/MM/dd");

编辑:使用插入: EDIT2:修正了错误: - )

var newString = dateString.Insert(4, "/").Insert(7, "/");

答案 4 :(得分:2)

只需使用字符串操作来插入斜杠:

string input = "20120321";

string dateString =
  input.Substring(0, 4) + "/" +
  input.Substring(4, 2) + "/" +
  input.Substring(6);

string dateString = input.Insert(6, "/").Insert(4, "/");

答案 5 :(得分:1)

如果是约会,请试试:

DateTime.ParseExact("20120321","yyyyMMdd", null).ToString("yyyy/MM/dd", System.Globalization.DateTimeFormatInfo.InvariantInfo)

答案 6 :(得分:1)

试试这个;

string dateString = DateTime.ParseExact("20120321", "yyyyMMdd",
                              null).ToShortDateString(); 

答案 7 :(得分:0)

如果您的数据始终采用相同的格式,并且您不需要对其进行验证,则可以使用以下代码段来避免使用DateTime解析它

var strWithInsert =  input.Insert(4,"/").Insert(7,"/");
相关问题