有人可以幫助我將字符串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. 將字符串日期時間轉換爲Ruby日期時間
- 3. 將日期時間字符串轉換爲日期時間
- 4. 轉換日期/時間字符串值到.NET日期時間
- 5. 將字符串轉換爲日期時間在C#中的錯誤.net
- 6. 將日期字符串轉換爲SSIS中的日期時間
- 7. C#字符串日期時間轉換
- 8. C#將字符串轉換爲無日期時間的日期時間
- 9. 在C中將字符串日期轉換爲系統日期時間#
- 10. 將字符串轉換爲日期C++
- 11. 將UTC日期轉換爲日期時間字符串Javascript
- 12. 將日期字符串轉換爲日期時間對象
- 13. 將日期時間轉換爲代表日期的字符串
- 14. 將日期字符串轉換爲日期時間
- 15. 將字符串轉換爲日期時間C#
- 16. 將SQL日期時間轉換爲C#字符串
- 17. 將日期時間轉換爲字符串C#
- 18. 將字符串轉換爲SqlServer的日期時間用C#
- 19. C#ASP.NET:無法將日期時間轉換爲字符串
- 20. 將字符串轉換爲日期時間c#
- 21. C#將字符串轉換爲日期時間
- 22. 將字符串轉換爲日期時間C#
- 23. 使用EdmFunction c將字符串轉換爲日期時間#
- 24. c#將字符串轉換爲日期時間
- 25. javascript日期/時間字符串轉換爲SQL日期時間
- 26. 轉換ISO 8601日期字符串到.NET日期時間
- 27. 將字符串轉換爲日期時間格式和日期只在C#
- 28. 在python 3中將字符串轉換爲日期時間
- 29. 在Python中將字符串轉換爲日期時間
- 30. 在asp.net中將字符串轉換爲日期時間
你是什麼意思*空值,而不會丟失的時間格式*? – 2010-12-05 20:14:04