美文网首页
iOS 常用的方法----时间戳/字符等转换(二)

iOS 常用的方法----时间戳/字符等转换(二)

作者: simuty | 来源:发表于2016-07-21 09:40 被阅读0次
    1. NSData转NSString
    + (NSString *)convertDataToString:(NSData *)aData
    {
        NSString *strResult = [[NSString alloc] initWithData:aData encoding:NSUTF8StringEncoding];
        return strResult;
    }
    
    
    2. NSString转NSData
    + (NSData *)convertStringToData:(NSString *)aString
    {
        NSData* dataResult = [aString dataUsingEncoding:NSUTF8StringEncoding];
        return dataResult;
    }
    
    
    3. NSDate转NSString
    + (NSString *)convertDateToString:(NSDate *)aDate
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSString *strDate = [dateFormatter stringFromDate:aDate];
        
        return strDate;
    }
    
    
    4. NSString转NSDate
    
    + (NSDate *)convertStringToDate:(NSString *)aString
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSDate *date = [dateFormatter dateFromString:aString];
        
        return date;
    }
    
    
    5. 获取当前时间
    + (NSString *)getNowDateTime
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSString *nowDateTime = [dateFormatter stringFromDate:[NSDate date]];
        return nowDateTime;
    }
    
    
    6. 把时间转换为时间戳
    + (NSString *)getNowTimeSp
    {
    
     NSDate *localDate = [NSDate date];
     NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[localDate timeIntervalSince1970]];
     //时间戳的值
     // NSLog(@"timeSp:%@",timeSp); 
      return timeSp;
    }
    
    
    7. 把时间戳转换为时间
    + (NSDate *)getTimeSpToDateWithStr:(NSString *)timeSp
    {
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeSp];
    return  NSDate;
    }    
           
    

    更多精彩内容请关注“IT实战联盟”哦~~~


    IT实战联盟.jpg

    相关文章

      网友评论

          本文标题:iOS 常用的方法----时间戳/字符等转换(二)

          本文链接:https://www.haomeiwen.com/subject/yxlbjttx.html