一、根据long类型
时间戳
转换周几
转自: http://www.ithao123.cn/content-7273821.html
写项目过程中,遇到一个问题,要求我把一个时间戳给显示为周几的样式, 百度了一下,完成之后就写一个博客记录下来. 下面是代码
二、根据NSString类型时间戳转换为日期格式
转自: http://www.ithao123.cn/content-7273821.html
写项目过程中,遇到一个问题,要求我把一个时间戳给显示为周几的样式, 百度了一下,完成之后就写一个博客记录下来. 下面是代码
//根据时间戳获取星期几
+ (NSString *)getWeekDayFordate:(long long)data
{
NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:data];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:newDate];
NSString *weekStr = [weekday objectAtIndex:components.weekday];
return weekStr;
}
二、根据NSString类型时间戳转换为日期格式
+ (NSString *)getDateAccordingTime:(NSString *)aTime formatStyle:(NSString *)formate{
NSDate *nowDate = [NSDate dateWithTimeIntervalSince1970:[aTime intValue]];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:formate];
return[formatter stringFromDate:nowDate];
}
PS: formate 返回时可以写成 @"YYYY-MM-HH HH:MM:SS"