有人可以幫助我將字符串14/04/2010 10:14:49.PM轉換爲C#.net中的datetime而不會丟失時間格式嗎?在C#.net中將字符串轉換爲日期時間
5
A
回答
0
1
DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy hh:mm:ss");
5
var date = DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy hh:mm:ss.tt", null);
對於字符串表示使用
date.ToString(@"dd/MM/yyyy hh:mm:ss.tt");
你也可以創建像extention方法這個:
public enum MyDateFormats
{
FirstFormat,
SecondFormat
}
public static string GetFormattedDate(this DateTime date, MyDateFormats format)
{
string result = String.Empty;
switch(format)
{
case MyDateFormats.FirstFormat:
result = date.ToString("dd/MM/yyyy hh:mm:ss.tt");
break;
case MyDateFormats.SecondFormat:
result = date.ToString("dd/MM/yyyy");
break;
}
return result;
}
0
使用轉換功能
using System;
using System.IO;
namespace stackOverflow
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine(Convert.ToDateTime("14/04/2010 10:14:49.PM"));
Console.Read();
}
}
}
0
我建議使用DateTime.ParseExact
作爲Parse
方法略有不同,根據當前線程的區域設置的行爲。
DateTime.ParseExact(yourString,
"dd/MM/yyyy hh:mm:ss.tt", null)
3
DateTime result =DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy HH:mm:ss.tt",null);
現在,您可以看到PM或AM和格式提供
相關問題
- 1. 如何在.NET中將字符串轉換爲日期時間?
- 2. 將日期時間字符串轉換爲日期時間
- 3. 轉換日期/時間字符串值到.NET日期時間
- 4. C#字符串日期時間轉換
- 5. C#將字符串轉換爲無日期時間的日期時間
- 6. 在C中將字符串日期轉換爲系統日期時間#
- 7. 將字符串轉換爲日期C++
- 8. 將日期字符串轉換爲日期時間
- 9. 將日期字符串轉換爲日期時間對象
- 10. 將UTC日期轉換爲日期時間字符串Javascript
- 11. 將字符串轉換爲日期時間C#
- 12. C#ASP.NET:無法將日期時間轉換爲字符串
- 13. 使用EdmFunction c將字符串轉換爲日期時間#
- 14. 將字符串轉換爲日期時間C#
- 15. 將日期時間轉換爲字符串C#
- 16. 將字符串轉換爲SqlServer的日期時間用C#
- 17. 在Python中將字符串轉換爲日期時間
- 18. 在asp.net中將字符串轉換爲日期時間
- 19. 將字符串'時間'轉換爲日期時間
- 20. 將字符串轉換爲.net中的日期.net
- 21. 將時間字符串轉換爲c#中的不同日期時間格式
- 22. 如何時間戳字符串轉換爲日期在C#
- 23. 字符串轉換爲日期時間在C#與EDT末
- 24. 將字符串轉換爲SSIS中的日期時間
- 25. 將字符串轉換爲SSMS中的日期時間
- 26. 將字符串轉換爲PHP中的日期時間
- 27. 將字符串轉換爲Javascript中的日期時間格式
- 28. 將字符串轉換爲Hive中的日期/時間戳
- 29. 如何將字符串轉換爲Excel中的日期/時間?
- 30. 將字符串轉換爲日期並將日期轉換回字符串
你是什麼意思*空值,而不會丟失的時間格式*? – 2010-12-05 20:14:04