//字典转字符串
+(NSString *)stringWithDict:(NSDictionary *)dict
{
NSError *parseError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&parseError];
if (parseError) {
NSLog(@"stringWithDict: /n%@",parseError);
}
return [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}
//字符串转字典
+(NSDictionary *)dictWithString:(NSString *)string
{
NSData *data=[string dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"dictWithString: /n%@",error);
}
return dict;
}
//字符串转日期
+(NSDate *)dateWithString:(NSString *)string timeStringFormat:(NSString *)format
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:NSLocalizedString(format,nil)];
NSDate *date = [formatter dateFromString:string];
return date;
}
//日期转字符串
+(NSString *)stringWithDate:(NSDate *)date timeStringFormat:(NSString *)format
{
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:NSLocalizedString(format,nil)];
NSString *nowDate=[formatter stringFromDate:date];
return nowDate;
}
//计算某个时间到目前时间差(XX 分钟/小时/天 前,)
+(int)numberOfTimeFromTodayByTime:(NSDate *)time timeType:(time_type)timetype
{
NSTimeInterval old=[time timeIntervalSince1970];
NSDate *nowDate=[NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval now=[nowDate timeIntervalSince1970]*1;
NSTimeInterval cha=now-old;
NSString *timeString=@"";
if (timetype==minute) {
timeString=[NSString stringWithFormat:@"%f",cha/60];
}
if (timetype==hour) {
timeString=[NSString stringWithFormat:@"%f",cha/3600];
}
if (timetype==day) {
timeString=[NSString stringWithFormat:@"%f",cha/86400];
}
timeString=[timeString substringToIndex:timeString.length-7];
return timeString.intValue;
}
网友评论