将日期(字符串)转换为另一种日期格式(字符串)

发布于 2024-08-08 18:01:18 字数 1256 浏览 2 评论 0原文

我有一个新手问题,如果有人可以帮助我,我会很高兴。

例如,我有一个 NSMutableArray:

{
JobID = 109302930;
Subject = "Test Subject";
SubmitDate = "2009-09-15 17:27:34";
}

我现在尝试在表视图中创建部分,并希望根据“格式化”SubmitDate 对我的记录进行分组。例如;

16th May'09
===========
Item A
Item B

18th May'09
===========
Item C
Item G

20th May'09
===========
Item Z
Item K

我使用以下函数(来自 XML 数据源)创建此 NSMutableArray:

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {   
    currentElement = [[NSString alloc] init];

    if ([elementName isEqualToString:@"subject"]) {
        [item setObject:currentSubject forKey:@"Subject"];
    } else if ([elementName isEqualToString:@"job_id"]) {
        [item setObject:currentJobID forKey:@"JobID"];
    } else if ([elementName isEqualToString:@"submit_date"]) {
        // I want to insert the date value in a formatted way instead of the standard MySQL date format (YYYY-MM-HH).
        [item setObject:currentJobSubmitDate forKey:@"SubmitDate"];
        [previewData addObject:item];
    }
}

正如我在上面的代码中注释的那样,我想将日期添加为格式化日期字符串,而不是 YYYY-MM-DD HH:II:SS。我怎样才能做到这一点?

感谢您的帮助。

I have a novice question, will be happy if any one out there can help me.

I have an NSMutableArray, for example:

{
JobID = 109302930;
Subject = "Test Subject";
SubmitDate = "2009-09-15 17:27:34";
}

I am now trying to create sections in my table view and want to group my records based on "formatted" SubmitDate. For example;

16th May'09
===========
Item A
Item B

18th May'09
===========
Item C
Item G

20th May'09
===========
Item Z
Item K

I am creating this NSMutableArray with the following function (from an XML data source):

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {   
    currentElement = [[NSString alloc] init];

    if ([elementName isEqualToString:@"subject"]) {
        [item setObject:currentSubject forKey:@"Subject"];
    } else if ([elementName isEqualToString:@"job_id"]) {
        [item setObject:currentJobID forKey:@"JobID"];
    } else if ([elementName isEqualToString:@"submit_date"]) {
        // I want to insert the date value in a formatted way instead of the standard MySQL date format (YYYY-MM-HH).
        [item setObject:currentJobSubmitDate forKey:@"SubmitDate"];
        [previewData addObject:item];
    }
}

As I commented in the above code, instead of YYYY-MM-DD HH:II:SS, I want to add the date as a formatted date string. How can I do that?

Thanks for your help.

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

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

发布评论

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

评论(1

花桑 2024-08-15 18:01:18

您需要使用 NSDateFormatter 来做到这一点。您可以使用它创建一个 NSDate,然后再次使用它来创建要使用的不同格式的字符串。

You'll want to use NSDateFormatter to do that. You can use it to create an NSDate, then use it again to create a differently-formatted string to use.

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