码迷,mamicode.com
首页 > 其他好文 > 详细

Objective-C日期相关工具方法

时间:2014-08-06 14:43:41      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   ar   cti   

//date根据formatter转换成string
+(NSString*)dateToString:(NSString *)formatter date:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    return[dateFormatter stringFromDate:date];
}

//string类型的时间戳转换成时间
+(NSString*)dateStringToString:(NSString *)dateStr
{
    if ([dateStr isKindOfClass:[NSNull class]]) {
        return nil;
    } else {
        NSInteger time = [dateStr intValue];
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
        NSString *timestr = [NSString stringWithFormat:@"%@",date];
        NSRange rang;
        rang.location = 0;
O        rang.length = 10;
        NSString *needtime = [timestr substringWithRange:rang];
        return needtime;
    }
}

//将日期从原格式转换成需要的格式
+(NSString*)convertDateFormatter:(NSString*)sourceFormatter targetFormatter:(NSString*)targetFormatter dateString:(NSString*)dateString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:sourceFormatter];
    NSDate *date = [dateFormatter dateFromString:dateString];
    [dateFormatter setDateFormat:targetFormatter];
    return[dateFormatter stringFromDate:date];
}

//将日期字符串转换成date
+(NSDate *)stringToDate:(NSString *)formatter dateString:(NSString *)dateString
{
    NSDateFormatter *dateFormatter= [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    return [dateFormatter dateFromString:dateString];
}



Objective-C日期相关工具方法,布布扣,bubuko.com

Objective-C日期相关工具方法

标签:style   blog   color   os   io   for   ar   cti   

原文地址:http://www.cnblogs.com/gongchen/p/3894329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!