// // NSNumber+TimeStamp.m // jinxin // // Created by Gavin on 15/1/30. // Copyright (c) 2015年 jinxin. All rights reserved. // #import "NSNumber+TimeStamp.h" @implementation NSNumber (TimeStamp) +(NSString *)newTimeforNumber:(NSNumber *)num { NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000]; NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *componentsWeek = [calendar components:NSWeekdayCalendarUnit fromDate:date]; NSString *weekStr = [weekday objectAtIndex:componentsWeek.weekday]; if ([num isEqualToNumber:@0]||num==nil) { return @""; } NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]]; [components setHour:0]; [components setMinute:0]; [components setSecond:0]; NSDate *today = [cal dateFromComponents:components]; [components setHour:-24]; [components setMinute:0]; [components setSecond:0]; NSDate *yesterday = [cal dateFromComponents:components]; [components setHour:-24*7]; [components setMinute:0]; [components setSecond:0]; NSDate *beforYestoday = [cal dateFromComponents:components]; long long timeStapm1 = [today timeIntervalSince1970]*1000; long long timeStapm2 = [yesterday timeIntervalSince1970]*1000; long long timeStapm3 = [beforYestoday timeIntervalSince1970]*1000; // 2.格式化日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; if (num.longLongValue > timeStapm1) { formatter.dateFormat = @"aKK:mm"; } else if (num.longLongValue > timeStapm2) { formatter.dateFormat = @"昨天 aKK:mm"; } else if (num.longLongValue > timeStapm3) { formatter.dateFormat = [NSString stringWithFormat:@"%@ aKK:mm",weekStr]; } else { formatter.dateFormat = @"yy-MM-dd aKK:mm"; } NSString *time = [formatter stringFromDate:date]; NSRange rang = [time rangeOfString:@"下午00"]; if (rang.length) { time = [time stringByReplacingOccurrencesOfString:@"下午00" withString:@"中午12"]; } return time; } + (NSString *)timeFromNumer:(NSNumber *)num { if ([num isEqualToNumber:@0]||num==nil) { return @""; } NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]]; [components setHour:0]; [components setMinute:0]; [components setSecond:0]; NSDate *today = [cal dateFromComponents:components]; [components setHour:-24]; [components setMinute:0]; [components setSecond:0]; NSDate *yesterday = [cal dateFromComponents:components]; [components setHour:-48]; [components setMinute:0]; [components setSecond:0]; NSDate *beforYestoday = [cal dateFromComponents:components]; long long timeStapm1 = [today timeIntervalSince1970]*1000; long long timeStapm2 = [yesterday timeIntervalSince1970]*1000; long long timeStapm3 = [beforYestoday timeIntervalSince1970]*1000; NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000]; // 2.格式化日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; if (num.longLongValue > timeStapm1) { formatter.dateFormat = @"HH:mm"; } else if (num.longLongValue > timeStapm2) { formatter.dateFormat = @"昨天 HH:mm"; } else if (num.longLongValue > timeStapm3) { formatter.dateFormat = @"前天 HH:mm"; } else { formatter.dateFormat = @"MM-dd HH:mm"; } NSString *time = [formatter stringFromDate:date]; return time; } + (NSString *)scheduleManagementDetailWith:(NSNumber *)num { NSString *dateString = [self strTimeFromNumber:num andDateFormat:@"yyyy/MM/dd"]; NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000]; NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *componentsWeek = [calendar components:NSCalendarUnitWeekday fromDate:date]; NSString *weekStr = [weekday objectAtIndex:componentsWeek.weekday]; if ([num isEqualToNumber:@0]||num==nil) { return @""; } NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitWeekOfMonth fromDate:[[NSDate alloc] init]]; [components setHour:0]; [components setMinute:0]; [components setSecond:0]; NSDate *today = [cal dateFromComponents:components]; [components setHour:-24]; [components setMinute:0]; [components setSecond:0]; NSDate *yesterday = [cal dateFromComponents:components]; [components setHour:24]; [components setMinute:0]; [components setSecond:0]; NSDate *tomorrow = [cal dateFromComponents:components]; long long timeStapm1 = [today timeIntervalSince1970]*1000; long long timeStapm2 = [yesterday timeIntervalSince1970]*1000; long long timeStapm3 = [tomorrow timeIntervalSince1970]*1000; NSString *todayString = [self strTimeFromNumber:@(timeStapm1) andDateFormat:@"yyyy/MM/dd"]; NSString *yesterdayString = [self strTimeFromNumber:@(timeStapm2) andDateFormat:@"yyyy/MM/dd"]; NSString *tomorrowString = [self strTimeFromNumber:@(timeStapm3) andDateFormat:@"yyyy/MM/dd"]; if ([dateString isEqualToString:todayString]) { return @"今天"; } else if ([dateString isEqualToString:yesterdayString] ) { return @"昨天"; }else if ([dateString isEqualToString:tomorrowString]) { return @"明天"; } else { return weekStr; } } + (NSString *)shortTimeFromNumer:(NSNumber *)num { NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000]; NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *componentsWeek = [calendar components:NSWeekdayCalendarUnit fromDate:date]; NSString *weekStr = [weekday objectAtIndex:componentsWeek.weekday]; if ([num isEqualToNumber:@0]||num==nil) { return @""; } NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]]; [components setHour:0]; [components setMinute:0]; [components setSecond:0]; NSDate *today = [cal dateFromComponents:components]; [components setHour:-24]; [components setMinute:0]; [components setSecond:0]; NSDate *yesterday = [cal dateFromComponents:components]; [components setHour:-24 * 7]; [components setMinute:0]; [components setSecond:0]; NSDate *beforYestoday = [cal dateFromComponents:components]; long long timeStapm1 = [today timeIntervalSince1970]*1000; long long timeStapm2 = [yesterday timeIntervalSince1970]*1000; long long timeStapm3 = [beforYestoday timeIntervalSince1970]*1000; // 2.格式化日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; if (num.longLongValue > timeStapm1) { formatter.dateFormat = @"aKK:mm"; } else if (num.longLongValue > timeStapm2) { return @"昨天"; // formatter.dateFormat = @"昨天 HH:mm"; } else if (num.longLongValue > timeStapm3) { return weekStr; } else { formatter.dateFormat = @"yy/MM/dd"; } NSString *time = [formatter stringFromDate:date]; NSRange rang = [time rangeOfString:@"下午00"]; if (rang.length) { time = [time stringByReplacingOccurrencesOfString:@"下午00" withString:@"中午12"]; } return time; } + (NSString *)hourAndMinuteWith:(NSNumber *)num { if ([num isEqualToNumber:@0]||num==nil) { return @""; } NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000]; // 2.格式化日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"HH:mm"; NSString *time = [formatter stringFromDate:date]; return time; } + (NSString *)stdTimeFromNumer:(NSNumber *)num { if ([num isEqualToNumber:@0]||num==nil) { return @""; } NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000]; // 2.格式化日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"MM-dd HH:mm:ss"; NSString *time = [formatter stringFromDate:date]; return time; } +(NSString *)strYMDTimeFromNumber:(NSNumber *)num { if ([num isEqualToNumber:@0]||num==nil) { return @""; } NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000]; // 2.格式化日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy年MM月dd日"; NSString *time = [formatter stringFromDate:date]; return time; } + (NSNumber *)currentTimeStamp { NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0]; NSTimeInterval a = [dat timeIntervalSince1970]*1000; long long num = llround(a); NSNumber *time = [NSNumber numberWithLongLong:num]; return time; } +(NSString *)strTimeFromNumber:(NSNumber *)num { if ([num isEqualToNumber:@0]||num==nil) { return @""; } NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000]; // 2.格式化日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy年MM月dd日 HH:mm"; NSString *time = [formatter stringFromDate:date]; return time; } +(NSString *)strTimeFromNumber:(NSNumber *)num andDateFormat:(NSString *)dateFormat{ if ([num isEqualToNumber:@0]||num==nil) { return @""; } NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000]; // 2.格式化日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = dateFormat; NSString *time = [formatter stringFromDate:date]; return time; } + (NSNumber *)changeNumberWithDate:(NSDate *)date { NSTimeInterval a = [date timeIntervalSince1970]*1000; long long num = llround(a); return [NSNumber numberWithLongLong:num]; } + (NSString *)YMDFromNumer:(NSNumber *)num { if ([num isEqualToNumber:@0]||num==nil) { return @""; } NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]]; [components setHour:0]; [components setMinute:0]; [components setSecond:0]; NSDate *today = [cal dateFromComponents:components]; [components setHour:-24]; [components setMinute:0]; [components setSecond:0]; NSDate *yesterday = [cal dateFromComponents:components]; [components setHour:-48]; [components setMinute:0]; [components setSecond:0]; NSDate *beforYestoday = [cal dateFromComponents:components]; long long timeStapm1 = [today timeIntervalSince1970]*1000; long long timeStapm2 = [yesterday timeIntervalSince1970]*1000; long long timeStapm3 = [beforYestoday timeIntervalSince1970]*1000; NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000]; // 2.格式化日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; if (num.longLongValue >= timeStapm1) { formatter.dateFormat = @"今天 "; } else if (num.longLongValue >= timeStapm2) { formatter.dateFormat = @"昨天 "; } else if (num.longLongValue >= timeStapm3) { formatter.dateFormat = @"前天 "; } else { formatter.dateFormat = @"MM-dd "; } NSString *time = [formatter stringFromDate:date]; return time; } +(NSDate *)changeDateStringWith:(NSString *)timeString { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 //设置时区,这个对于时间的处理有时很重要 //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算. //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢? //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了 // NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [formatter setTimeZone:timeZone]; return [formatter dateFromString:timeString]; } +(NSDate *)changeDateStringWith:(NSString *)timeString andFormatter:(NSString *)formatterString { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:formatterString]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 //设置时区,这个对于时间的处理有时很重要 //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算. //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢? //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了 NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; [formatter setTimeZone:timeZone]; return [formatter dateFromString:timeString]; } +(NSDate *)changeDateHmsStringWith:(NSString *)timeString{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 //设置时区,这个对于时间的处理有时很重要 //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算. //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢? //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了 // NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [formatter setTimeZone:timeZone]; return [formatter dateFromString:timeString]; } + (NSNumber *)timeStampFromString:(NSString *)dateString { NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date = [dateFormatter dateFromString:dateString]; NSTimeInterval time = [date timeIntervalSince1970]*1000; return [NSNumber numberWithLongLong:time]; } @end