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

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实战联盟”哦~~~


iOS 常用的方法----时间戳/字符等转换(二)_第1张图片
IT实战联盟.jpg

你可能感兴趣的:(iOS 常用的方法----时间戳/字符等转换(二))