有人可以帮我转换字符串14/04/2010 10:14:49.PM到C#.net的日期时间而不会丢失时间格式吗?
var date = DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy hh:mm:ss.tt", null);
For string representation use
date.ToString(@"dd/MM/yyyy hh:mm:ss.tt");
Also you can create extention method like this:
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;
}
DateTime result =DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy HH:mm:ss.tt",null);
您现在可以看到格式提供程序的PM或AM和null值
DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy hh:mm:ss");
DateTime.Parse(@"14/04/2010 10:14:49.PM");
应该工作,而不是在VS附近,所以我不能尝试
Use convert function
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();
}
}
}
I recommend using DateTime.ParseExact
as the Parse
method behaves slightly differently according to the current thread locale settings.
DateTime.ParseExact(yourString,
"dd/MM/yyyy hh:mm:ss.tt", null)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.