将自定义日期格式(字符串)转换为日期时间 [英] Converting a custom date format (string) to a datetime
问题描述
我有一个大集(1亿个)观察结果,日期表示为自定义字符串格式。我们没有生成日期字符串,我只需要将日期字符串转换为日期时间类型。
I have a large set (100+ million) of observations with the date represented as a custom string format. We did not generate the date strings, I just need to convert the date string to a datetime type.
如何将字符串日期(例如2010年12月12日)转换为日期时间?我的想法是将字符串解析成组件,以正确的顺序组合它们,并将该字符串传递给datetime的构造函数。有没有办法传递datetime()一个自定义日期格式字符串?
How do I convert a string date (eg, 12 JUN 2010) to a datetime? My thoughts are to parse the string into components, assemble them in the correct order, and pass that string to the constructor of datetime. Is there a way to pass datetime() a custom date format string instead?
谢谢!
推荐答案
查看 DateTime .ParseExact
,例如
var dateTime = DateTime.ParseExact(
"12 JUN 2010",
"dd MMM yyyy",
CultureInfo.InvariantCulture);
您还可以指定第四个参数来设置日期/时间的,例如,如果它们是UTC日期/时间,那么你可能想要指定 DateTimeStyles.AssumeUniversal
。
You can also specify a fourth parameter to set the Kind of date/time, for example if they are UTC date/times then you'd likely want to specify DateTimeStyles.AssumeUniversal
.
这篇关于将自定义日期格式(字符串)转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!