2010-12-05 115 views

回答

0
DateTime.Parse(@"14/04/2010 10:14:49.PM"); 

应该工作,不要靠近VS此刻,所以我不能尝试

+0

这有一个模糊的日期/月格式,所以在某些情况下不起作用 – Myster 2010-12-05 20:18:33

+0

我同意Myster。行为是与语言环境相关的。 – jdehaan 2010-12-05 20:23:59

+0

假设这是DateTimeFormatInfo.CurrentInfo的正确格式,这应该起作用。 – MerickOWA 2010-12-05 20:24:52

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和格式提供

相关问题