123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- //
- // 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
|