在c#中将时间戳字符串转换为DateTime对象

在c#中将时间戳字符串转换为DateTime对象

问题描述:

我在下面的格式 2012年5月1日下午三时38分27秒时间戳的字符串。我该如何将它转换为DateTime对象在C#

I have timestamp strings in the following format 5/1/2012 3:38:27 PM. How do I convert it to a DateTime object in c#

您输入的字符串看起来像恩我们格式,这是 M / D / YYYY H /毫米/ SS TT 。你必须在分析使用正确的的CultureInfo 实例:

You input string looks like in en-us format, which is M/d/yyyy h/mm/ss tt. You have to use proper CultureInfo instance while parsing:

var ci = System.Globalization.CultureInfo.GetCultureInfo("en-us");

var value = DateTime.Parse("5/1/2012 3:38:27 PM", ci);

var ci = new System.Globalization.CultureInfo("en-us");