首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将字符串转换为NSDate

将字符串转换为NSDate
EN

Stack Overflow用户
提问于 2010-02-22 22:24:46
回答 5查看 76.6K关注 0票数 69

如何在iOS上将字符串转换为NSDate

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-02-23 11:53:17

代码语言:javascript
复制
NSString *dateStr = @"20100223";

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];  

// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];  
[dateFormat release];

希望这能对你有所帮助。

票数 197
EN

Stack Overflow用户

发布于 2010-02-22 22:27:55

你会想看看NSDateFormatter。确定日期字符串的格式,然后使用dateFromString:将该字符串转换为NSDate对象。

票数 9
EN

Stack Overflow用户

发布于 2015-03-13 15:08:20

代码语言:javascript
复制
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/YYYY"];
NSDate *date = [dateFormat dateFromString:dateStr];  
list = [self getYear:date];


- (NSMutableArray *)getYear:(NSDate*)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];

    int year = [components year];
    int month = [components month];
    int day = [components day];
    NSLog(@"%d",year);

    NSMutableDictionary *dateDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d", day], @"day", [NSString stringWithFormat:@"%d", month], @"month", [NSString stringWithFormat:@"%d", year], @"year", nil];

    return dateDict;
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2311421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档