在 iPad 应用程序上将 VB.Net 生成的日期字符串转换为 NSDate

发布于 2024-12-25 21:41:57 字数 801 浏览 3 评论 0 原文

我有一个字符串格式的日期,该日期是由 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];

但是我的想法看起来太过分了,所以我认为我缺少一些更简单的方法来做到这一点:(

感谢您提供任何更简单的方法来改变它!

I have a date in string format the date is generated by VB.Net application and persisted into XML file which than I am parsing it onto iPad. The parsed content has date in string like,

"Monday, 07 November 2011 16:03"

So, the question is..

How can I convert this format to NSDate object? I couldn't found any method in documentation.


Effort So Far

I thought of separating string component and than evaluate each component and convert them into numbers such as,

Monday = 0

Later I can create a date like this, i.e. by setting component.

 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];

However my idea looks overkill, So I think I am missing some easier way to do this :(

Thanks for any easier way to transform this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城旧梦 2025-01-01 21:41:57

使用 VB.NET 的日期输出格式创建一个 NSDateFormatter(如 UTS TR-35) 并使用 -dateFromString: 将其转换为 NSDate。例如,使用您的示例:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EEEE, dd MMMM yyyy HH:mm"];
NSDate *d = [df dateFromString:@"Monday, 07 November 2011 16:03"];
[df release];

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 an NSDate. For example, using your example:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EEEE, dd MMMM yyyy HH:mm"];
NSDate *d = [df dateFromString:@"Monday, 07 November 2011 16:03"];
[df release];
世态炎凉 2025-01-01 21:41:57

请参阅[NSDate dateWithNaturalLanguageString:]。它可能会满足您的需要。

See [NSDate dateWithNaturalLanguageString:]. It might do what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文