C# 将ddMMyyyy格式的字符串转换为日期时间

C# 将ddMMyyyy格式的字符串转换为日期时间,c#,datetime,C#,Datetime,如何将ddMMyyyy格式的字符串转换为DateTime?请参阅和 印刷品: 15072008 converts to 7/15/2008 12:00:00 AM. 尝试使用: 你可以很容易地做到这一点 下面是一个例子 String origionalDate = "12/20/2013"; // Format : MM/dd/yyyy string origionalFormat = "MM/dd/yyyy"; string convertInToFormat="dd

如何将ddMMyyyy格式的字符串转换为DateTime?

请参阅和

印刷品:

15072008 converts to 7/15/2008 12:00:00 AM.
尝试使用:


你可以很容易地做到这一点

下面是一个例子

    String origionalDate = "12/20/2013"; // Format : MM/dd/yyyy
    string origionalFormat = "MM/dd/yyyy";
    string convertInToFormat="dd/MM/yyyy";
    String convertedDate;
    DateTime objDT;

    if (DateTime.TryParseExact(origionalDate, origionalFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out objDT) == true)
    {
        convertedDate = objDT.ToString(convertInToFormat);
        Response.Write("<b>Original DateTime Format ( " + origionalFormat + " ) : </b>" + origionalDate);
        Response.Write("<br/>");
        Response.Write("<b>Converted DateTime Format ( " + convertInToFormat + " )  : </b>" + convertedDate);
    }
    else
    {
        Response.Write("<b>Not able to parse datetime.</b>");
    }
String origionalDate=“12/20/2013”//格式:年月日
字符串origionalFormat=“MM/dd/yyyy”;
字符串convertinotoformat=“dd/MM/yyyy”;
字符串转换日期;
日期时间objDT;
if(DateTime.TryParseExact(origionalDate、origionalFormat、CultureInfo.InvariantCulture、datetimestyle.None、out objDT)=true)
{
convertedDate=objDT.ToString(ConvertingToFormat);
响应.写入(“原始日期时间格式(“+OriginalFormat+”):“+OriginalDate”);
响应。写入(“
”); Write(“转换的日期时间格式(“+ConvertingToFormat+”):“+convertedDate”); } 其他的 { Write(“无法解析日期时间”); }
更具体地说,
DateTime.ParseExact()
您也可以使用
TryParseExact()
而不是捕获异常。visaversa呢?
DateTime.ParseExact(yourDateString, "ddMMyyyy", CultureInfo.InvariantCulture);
    String origionalDate = "12/20/2013"; // Format : MM/dd/yyyy
    string origionalFormat = "MM/dd/yyyy";
    string convertInToFormat="dd/MM/yyyy";
    String convertedDate;
    DateTime objDT;

    if (DateTime.TryParseExact(origionalDate, origionalFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out objDT) == true)
    {
        convertedDate = objDT.ToString(convertInToFormat);
        Response.Write("<b>Original DateTime Format ( " + origionalFormat + " ) : </b>" + origionalDate);
        Response.Write("<br/>");
        Response.Write("<b>Converted DateTime Format ( " + convertInToFormat + " )  : </b>" + convertedDate);
    }
    else
    {
        Response.Write("<b>Not able to parse datetime.</b>");
    }