将数组中的 NSString 转换为 NSDates

发布于 2024-11-05 01:50:12 字数 229 浏览 0 评论 0原文

我正在做一项任务,允许用户显示“今天”发生的事件。我已经解析了 XML 文件并将内容存储到一个数组中。 XML 文件的内容由标题、描述、日期等组成。日期采用 NSString 格式,我想将它们转换为 NSDates 并与今天的日期进行比较,然后再将它们显示在 UITableView 中。

我是 obj-c 的新手,我在网上搜索了 NSDate 的帮助,但找不到我需要的东西。对此的任何链接、建议或帮助都非常感谢。提前致谢 (:

I'm working on an assignment that allows the user to display events that are happening 'today'. I have parsed the XML file and stored the contents into an array. The contents of the XML file consists of a title, description, date etc. The dates are in NSString format and I want to convert them into NSDates and compare them with today's date before displaying them in a UITableView.

I'm new to obj-c and I've searched online for help on NSDate, but I couldn't find what I need. Any links, advice or help on this is really appreciated. Thanks in advance (:

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

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

发布评论

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

评论(4

十级心震 2024-11-12 01:50:12

假设 dateString 包含字符串格式的日期,

首先从字符串中获取日期:-

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
   [formatter setDateFormat:@"dd/mm/yyyy"];
  NSDate *dateprevious = [formatter dateFromString:dateString];

现在获取今天的日期

    NSDate *date=[NSDate date]; 
[formatter setDateFormat:@"dd"];
    NSString *dateOfGame =[formatter stringFromDate:dateprevious];
    NSString *todaydate =[formatter stringFromDate:date];
[formatter release];

if([todaydate isEqualToString:dateknown])
{
NSLog(@"date matched");
}

suppose dateString contains the date in string format

first get date from string:-

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
   [formatter setDateFormat:@"dd/mm/yyyy"];
  NSDate *dateprevious = [formatter dateFromString:dateString];

Now get today date

    NSDate *date=[NSDate date]; 
[formatter setDateFormat:@"dd"];
    NSString *dateOfGame =[formatter stringFromDate:dateprevious];
    NSString *todaydate =[formatter stringFromDate:date];
[formatter release];

if([todaydate isEqualToString:dateknown])
{
NSLog(@"date matched");
}

根据字符串的格式,您可以使用 this

+ (id)dateWithNaturalLanguageString:(NSString *)string

要比较两个日期,您会发现这里有很多有用的答案:)

Depending on the format of the string, you can use this:

+ (id)dateWithNaturalLanguageString:(NSString *)string

To compare two dates you will find here a lot of usefull answers :)

憧憬巴黎街头的黎明 2024-11-12 01:50:12
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy , hh:mm a"];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [[dateFormatter datefromString:date] retain];
[dateFormatter release];

你可以用这个

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy , hh:mm a"];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [[dateFormatter datefromString:date] retain];
[dateFormatter release];

You can use this one

战皆罪 2024-11-12 01:50:12

看看 NSDateFormatter

它有一个名为 dateFromString 的方法
就像你可以执行以下操作:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/mm/yyyy"];
NSDate *date = [formatter dateFromString:@"5/5/2011"];

Have a look at NSDateFormatter

It has a method called dateFromString
Like you could do the following:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/mm/yyyy"];
NSDate *date = [formatter dateFromString:@"5/5/2011"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文