在 iPad 应用程序上将 VB.Net 生成的日期字符串转换为 NSDate
我有一个字符串格式的日期,该日期是由 VB.Net 应用程序生成的,并保存到 XML 文件中,然后我将其解析到 iPad 上。解析的内容在字符串中包含日期,例如,
"Monday, 07 November 2011 16:03"
所以问题是..
如何将此格式转换为 NSDate
对象?我在文档中找不到任何方法。
到目前为止的努力
我想到了分离字符串组件,然后评估每个组件并将它们转换为数字,例如,
Monday = 0
稍后我可以创建这样的日期,即通过设置组件。
NSDate *now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now];
[comp setWeekday:1];
[comp setWeek: 21];
NSDate *resultDate = [gregorian dateFromComponents:comp];
但是我的想法看起来太过分了,所以我认为我缺少一些更简单的方法来做到这一点:(
感谢您提供任何更简单的方法来改变它!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 VB.NET 的日期输出格式创建一个
NSDateFormatter
(如 UTS TR-35) 并使用-dateFromString:
将其转换为NSDate
。例如,使用您的示例:Create an
NSDateFormatter
with the format of VB.NET's date output (as specified in UTS TR-35) and use-dateFromString:
to convert it into anNSDate
. For example, using your example:请参阅
[NSDate dateWithNaturalLanguageString:]
。它可能会满足您的需要。See
[NSDate dateWithNaturalLanguageString:]
. It might do what you need.