NSString解析查找日期(NSString parse to find date)

编程入门 行业动态 更新时间:2024-10-27 00:25:27
NSString解析查找日期(NSString parse to find date)

我从包含格式奇怪的日期的数据库中提取信息。

当他们被拉出时,他们的格式为:

DayOfWeek,月份日期

我正在尝试使用EventKit来选择将日期添加到用户日历。

我似乎无法找到最好的方法去做这件事。

任何帮助或正确方向的点将非常感谢!!

I am pulling information from a database that contains dates formatted weird.

When they are pulled they are in the format of:

DayOfWeek, Month Date

I am attempting to use EventKit to have the option to add the date to the users calendar.

I can't seem to find the best way to go about doing this.

Any help or point in the right direction would be very much appreciated!!

最满意答案

您的问题是此数据库的日期不明确。 它没有年份信息。 NSDateFormatters不猜测日期信息。 他们根据您提供的信息创建日期。 由于缺少这些信息,年份将是1970年(如:与参考数据同年)。

因为数据库中保存的格式完全是愚蠢的,所以我假设这些日期总是在接下来的365天内。 所以理论上你不需要保存一年的信息。 然后你可以使用这样的东西从完全模糊的日期信息中找出NSDate。 我们的想法是将日期从1970年(从您的字符串创建)转移到当前年份。 如果它是过去一年的过去(例如今天是3月31日,“Foo,3月30日”的日期将会过去)将其移至明年。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"EEEE, MMMM d"; // the following line is important if you want that your code runs on device that are not english! dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; NSDate *date = [dateFormatter dateFromString:@"Wednesday, March 30"]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [calendar components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date]; NSDateComponents *todayComponent = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]]; NSInteger proposedYear = [todayComponent year]; // if supposed date would be in the past for the current year move it into next year if ([components month] < [todayComponent month]) { proposedYear++; } if ([components month] == [todayComponent month] && [components day] < [todayComponent day]) { proposedYear++; } [components setYear:proposedYear]; date = [calendar dateFromComponents:components]; // just for logging, so you are sure that you use the correct year: [dateFormatter setDateFormat:@"EEEE, MMMM d yyyy"]; NSLog(@"%@", [dateFormatter stringFromDate:date]);

Your problem is that this database has ambiguous dates. It does not have a year information. NSDateFormatters don't guess date information. They create dates from the information you provide. And since this information is missing the year will be 1970 (as in: the same year as the reference data).

Because the format that is saved in the database is totally stupid I assume that those dates always are within the next 365 days. So in theory you wouldn't have to save a year info. You could then use something like this to figure out the NSDate from that totally ambiguous date information. The idea is to transfer the date from 1970 (created from your string) to the current year. And if it would be in the past for current year (e.g. today is 31 March a date with "Foo, March 30" would be in the past) move it to next year.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"EEEE, MMMM d"; // the following line is important if you want that your code runs on device that are not english! dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; NSDate *date = [dateFormatter dateFromString:@"Wednesday, March 30"]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [calendar components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date]; NSDateComponents *todayComponent = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]]; NSInteger proposedYear = [todayComponent year]; // if supposed date would be in the past for the current year move it into next year if ([components month] < [todayComponent month]) { proposedYear++; } if ([components month] == [todayComponent month] && [components day] < [todayComponent day]) { proposedYear++; } [components setYear:proposedYear]; date = [calendar dateFromComponents:components]; // just for logging, so you are sure that you use the correct year: [dateFormatter setDateFormat:@"EEEE, MMMM d yyyy"]; NSLog(@"%@", [dateFormatter stringFromDate:date]);

更多推荐

本文发布于:2023-08-05 23:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441160.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   NSString   parse   date   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!