如何將ddMMyyyy格式的字符串轉換爲DateTime?將ddMMyyyy格式的字符串轉換爲DateTime
18
A
回答
39
嘗試使用DateTime.ParseExact
:
DateTime.ParseExact(yourDateString, "ddMMyyyy", CultureInfo.InvariantCulture);
3
見Parsing Date and Time和DateTime.ParseExact()
String dateString = "15072008";
String format = "ddMMyyyy";
try {
DateTime result = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) {
Console.WriteLine("{0} is not in the correct format.", dateString);
}
打印:
15072008 converts to 7/15/2008 12:00:00 AM.
1
你可以做到這一點很容易。
這裏是一個例子。
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>");
}
相關問題
- 1. 如何將字符串轉換爲特定格式的DateTime?
- 2. 如何將字符串轉換爲指定格式的DateTime?
- 3. 在vb.net中將字符串轉換爲datetime的標準格式
- 4. 將字符串轉換爲特定的DateTime格式
- 5. 將日期格式化的字符串轉換爲DateTime對象?
- 6. 將'YYYYMMDDHHMMSS'格式的字符串轉換爲datetime
- 7. 的Sybase:將字符串轉換爲datetime
- 8. 將字符串格式「yyyy-MM-ddTHH:mm:ss.fffZ」轉換爲DateTime
- 9. 使用yyyyMMdd格式化將字符串轉換爲DateTime
- 10. 在Python中將字符串轉換爲DateTime到UTC格式,postgresql
- 11. 將字符串轉換爲DateTime格式問題C#
- 12. 將字符串轉換爲格式爲
- 13. 字符串轉換爲DateTime
- 14. 字符串轉換爲DateTime
- 15. 將字符串轉換爲datetime問題
- 16. Powershell將字符串轉換爲datetime
- 17. 將字符串從xml轉換爲DateTime
- 18. 將字符串轉換爲DateTime C#ASP.NET
- 19. 將字符串轉換爲DateTime Ruby
- 20. 無法將字符串轉換爲DateTime?
- 21. 將字符串轉換爲DateTime Asp.Net
- 22. SQL Server將字符串轉換爲datetime
- 23. vb.net將此字符串轉換爲DateTime
- 24. 將字符串轉換爲DateTime?
- 25. 將字符串轉換爲datetime問題
- 26. 將DateTime類轉換爲字符串
- 27. 將字符串轉換爲DateTime對象
- 28. 將字符串轉換爲dateTime美國
- 29. 將2個字符串轉換爲DateTime
- 30. 將「9am」字符串轉換爲DateTime
更具體地說,'DateTime.ParseExact()' – Mark 2010-09-23 16:48:05
你也可以使用'TryParseExact()',而不是捕獲異常。 – 2010-09-23 16:56:50