将LDAP中的WhenChanged属性(Generalized-Time)转换为C#中的DateTime [英] Converting the WhenChanged attribute (Generalized-Time) in LDAP to a DateTime in C#
问题描述
你所得到的格式是接近圆旅行日期时间格式(o)和通用排序的往返日期时间格式(u)标准日期时间格式字符串,如这里。
一个kludgy解决方案将是按你符合该模式的字符串,然后使用 ParseExact 的o或u标准格式字符串。 p>
更好的方法是构建一个自定义格式字符串匹配您已经获得的数据。在标准日期时间格式字符串页面的标准格式字符串工作方式部分中,您将看到等同于o和u的完整自定义格式化字符串。这应该给你一个很好的开始。
编辑:添加代码
string format =yyyyMMddHHmmss.f'Z;
string target =20070828085401.0Z;
日期时间d = DateTime.ParseExact(target,format,CultureInfo.InvariantCulture);
I recently switch from using S.DS namespace (which uses ADSI) to the S.SD.Protocol namespace. The only problem is that ADSI handled the conversion of Generalized-Time to a DateTime for me. Now I'm getting back a value of "20070828085401.0Z" for the WhenChanged attribute. DateTime.Parse() will not convert this so is there another way?
The format you are getting is close to the round trip date time pattern ("o") and universal sortable round trip date time pattern ("u") standard date time format strings as described here.
One kludgy solution would be to massage the string you get to fit the pattern and then use the "o" or "u" standard format string with ParseExact.
A better way would be to construct a custom format string that matches the data you are already getting. In the "How Standard Format Strings Work" section of the standard date time format strings page you'll see the full custom formatting strings equivalent to "o" and "u". That should give you a good start.
EDIT: Add code
string format = "yyyyMMddHHmmss.f'Z'";
string target = "20070828085401.0Z";
DateTime d = DateTime.ParseExact(target, format, CultureInfo.InvariantCulture);
这篇关于将LDAP中的WhenChanged属性(Generalized-Time)转换为C#中的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!