将LDAP中的WhenChanged属性(Generalized-Time)转换为C#中的DateTime [英] Converting the WhenChanged attribute (Generalized-Time) in LDAP to a DateTime in C#

查看:1179
本文介绍了将LDAP中的WhenChanged属性(Generalized-Time)转换为C#中的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从使用S.DS命名空间(使用ADSI)切换到S.SD.Protocol命名空间。唯一的问题是ADSI处理了Generalized-Time到DateTime的转换。现在我为更改属性取回20070828085401.0Z的值。 DateTime.Parse()不会转换,所以有另一种方式?

解决方案

你所得到的格式是接近圆旅行日期时间格式(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆