English 中文(简体)
我如何确定在时间上使用的扼杀形式?
原标题:how can I identify the format of a string to be used with DateTime?
  • 时间:2012-01-13 19:38:02
  •  标签:
  • c#

I have a database script that returns date time string and my problem is that I don t know what format my string will be. The reason is that I have desktop in different culture and they could return any kind of format. My ultimate goal is, from the string to return the DateTime. As I am trying to write something I am realizing that I would need to try any kind of format to make sure something comes back to me without an exception. There should be a better way to do this without trial and error. This is what I have but it only works for a few formats:

    public static DateTime FromQueryResultString(string dttmString)
    {
        string[] formats = { "dd/MM/yyyy", "yyyy-MM-dd HH:mm:ss", "dd/MM/yyyy HH:mm:ss", "yyyyMMdd HH:mm:ss", "dd.MM.yy hh:mm", "M/d/yy h:mm tt", "ddd MMM dd H:mm:ss yyyy", "dd.MM.yy hh:mm", "dd.MM.yy HH:mm","ddd MMM yy H:mm:ss yyyy" };

        string name = Thread.CurrentThread.CurrentCulture.Name;
        IFormatProvider format = new CultureInfo(name, false);
        DateTime formattedDate = DateTime.ParseExact(dttmString, formats, format, DateTimeStyles.None);
    }
最佳回答

我认为,你没有基本概念。

考虑到你再次谈论数据基,它是一种文字,I immagine,它从数据库上获取数据。 页: 1 在这种情况下,你将有一个单一的稳定的数据存取层,它将回收并操纵从罗马、巴黎、北京或阿布扎比收集的数据。 The client program that data will pornography/em> in culture Depend way.

简言之,选择一种格式并以这种方式储存。

很显然,这一结构不可能有always<>em>,但从一成不变,在99%的案例中是可能的。

希望这一帮助。

问题回答

为此:

DateTime parsedDate;

if(DateTime.TryParse(dttmString, parsedDate)) 
{ 
      //TODO
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...