/**
* 时间戳转化为指定格式的时间
*
* @param timeStamp 时间戳如@"-28800"
* @param formate 如 yyyy-MM-dd HH-mm-ss
*
* @return 如1970-01-01
*/
+ (NSString *)timeWithTimeStamp:(NSString *)timeStamp andtimeFormate:(NSString *)formate
{
NSTimeInterval time= [timeStamp longLongValue];
NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:time];
NSDateFormatter *dateFormate = [[NSDateFormatter alloc]init];
dateFormate.dateFormat = formate;
dateFormate.timeZone = [NSTimeZone systemTimeZone];
[dateFormate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8 * 3600]];
NSString *str = [dateFormate stringFromDate:detaildate];
return str;
}
/**
* 时间戳转化为星期
*
* @param timeStamp 时间戳如@"-28800"
*
* @return 如星期一
*/
+ (NSString *)getWeekDayFordate:(long long)data
{
NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:(data / 1000)];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:newDate];
NSString *weekStr = [weekday objectAtIndex:components.weekday];
return weekStr;
}
+ (NSString *)showCurrentTime
{
//获取当前时间
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *currentTime = [formatter stringFromDate:[NSDate date]];
return currentTime;
}
/// 传入的时间与当前时间的时间差
+ (NSArray *)intervalSinceNow:(NSString *)theDate
{
NSArray *timeArray=[theDate componentsSeparatedByString:@"."];
theDate=[timeArray objectAtIndex:0];
NSDateFormatter *date=[[NSDateFormatter alloc] init];
[date setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[date setDateFormat:@"yyyy-MM-dd HH"];
// [date setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSDate *d = [date dateFromString:theDate];
NSTimeInterval late = [d timeIntervalSince1970];
NSDate *dat = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: dat];
NSDate *localeDate = [dat dateByAddingTimeInterval:interval];
NSTimeInterval now = [localeDate timeIntervalSince1970];
NSString *timeString = @"";
NSString *hourString = @"";
NSMutableArray *mArr = [[NSMutableArray alloc]init];
NSTimeInterval cha = late - now;
if (cha / 3600 > 1 && cha / 86400 < 1) {
hourString = [NSString stringWithFormat:@"%f", cha / 3600];
// hourString = [hourString substringToIndex:timeString.length - 7];
NSArray *array = [hourString componentsSeparatedByString:@"."];
hourString = array[0];
// timeString=[NSString stringWithFormat:@"剩余%@小时", timeString];
timeString = [NSString stringWithFormat:@"%@", @"0"];
}
if (cha / 86400 > 1)
{
NSString *string = [NSString stringWithFormat:@"%f", cha / 86400];
NSArray *array = [string componentsSeparatedByString:@"."];
timeString = array[0];
// timeString = [NSString stringWithFormat:@"剩余%@天", timeString];
NSString *hourStr;
if (array[1]) {
hourStr = array[1];
}else{
hourStr = @"0";
}
NSInteger hour =(NSInteger)([[NSString stringWithFormat:@"%@%@%@",@"0",@".",hourStr] doubleValue] * 24);
hourString = [NSString stringWithFormat:@"%ld",(long)hour];
}
[mArr addObject:timeString];
[mArr addObject:hourString];
return [mArr copy];
}