說明都在注釋里了,不解釋了摔刁。
NSDate+Covert.h
#import <Foundation/Foundation.h>
@interface NSDate (Covert)
//時(shí)間戳轉(zhuǎn)換為時(shí)間格式 yyyy-MM-dd
+ (NSString *)yyyyMMddStringWithSecond:(NSTimeInterval)time;
//獲取當(dāng)前時(shí)間
+(NSString*)getCurrentTimes;
//獲取當(dāng)前時(shí)間戳方法(以秒為單位)
+(NSString *)getNowTimeTimestamp;
//獲取當(dāng)前時(shí)間戳 (以毫秒為單位)
+(NSString *)getNowTimeTimestamp3;
//獲取30s后時(shí)間戳方法(以秒為單位),如果想要其它時(shí)間挥转,自己做下封裝
+(NSString *)get30sTimeTimestamp;
//一個(gè)時(shí)間戳距離當(dāng)前時(shí)間日分秒,常用于 10秒前共屈,10分鐘前扁位,10小時(shí)前。趁俊。域仇。。
+(NSString *)twoTimestampSub:(NSString *)oneTime nowTime:(NSString *)nowTime;
//時(shí)間戳轉(zhuǎn)化為時(shí)間
+(NSString *)timeStampChangeTime:(NSString *)timeStamp andFormatter:(NSString *)Formatter;
//秒數(shù)轉(zhuǎn)化時(shí)分寺擂,例如:50:18
+(NSString *)timeChangeMS:(NSInteger)time;
@end
NSDate+Covert.m
#import "NSDate+Covert.h"
@implementation NSDate (Covert)
//時(shí)間戳轉(zhuǎn)換為時(shí)間格式 yyyy-MM-dd
+ (NSString *)yyyyMMddStringWithSecond:(NSTimeInterval)time {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy.MM.dd"];
return [dateFormat stringFromDate:date];
}
//獲取當(dāng)前時(shí)間
+(NSString*)getCurrentTimes{
return nil;
}
//獲取當(dāng)前時(shí)間戳有方法(以秒為單位)
+(NSString *)getNowTimeTimestamp{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
//設(shè)置時(shí)區(qū),這個(gè)對(duì)于時(shí)間的處理有時(shí)很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間,你可以輸出來看下是什么格式
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
return timeSp;
}
//獲取當(dāng)前時(shí)間戳 (以毫秒為單位)
+(NSString *)getNowTimeTimestamp3{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"]; // ----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
//設(shè)置時(shí)區(qū),這個(gè)對(duì)于時(shí)間的處理有時(shí)很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間,你可以輸出來看下是什么格式
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
return timeSp;
}
//獲取30s后時(shí)間戳方法(以秒為單位)
+(NSString *)get30sTimeTimestamp{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
//設(shè)置時(shí)區(qū),這個(gè)對(duì)于時(shí)間的處理有時(shí)很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間,你可以輸出來看下是什么格式
NSTimeInterval oneDay = 30; // 一天一共有多少秒
NSDate * appointDate = [datenow initWithTimeIntervalSinceNow: oneDay];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[appointDate timeIntervalSince1970]];
return timeSp;
}
//一個(gè)時(shí)間戳距離當(dāng)前時(shí)間日分秒
+(NSString *)twoTimestampSub:(NSString *)oneTime nowTime:(NSString *)nowTime{
// 1.確定時(shí)間
// NSString *time1 = @"2015-06-23 12:18:15";
// NSString *time2 = @"2015-06-28 10:10:10";
NSDate *date11 = [NSDate dateWithTimeIntervalSince1970:[oneTime doubleValue]];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *time1 = [formatter1 stringFromDate: date11];
NSDate *date22 = [NSDate dateWithTimeIntervalSince1970:[nowTime doubleValue]];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *time2 = [formatter2 stringFromDate: date22];
// 2.將時(shí)間轉(zhuǎn)換為date
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date1 = [formatter dateFromString:time1];
NSDate *date2 = [formatter dateFromString:time2];
// 3.創(chuàng)建日歷
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 4.利用日歷對(duì)象比較兩個(gè)時(shí)間的差值
NSDateComponents *cmps = [calendar components:type fromDate:date1 toDate:date2 options:0];
// 5.輸出結(jié)果
// NSLog(@"兩個(gè)時(shí)間相差%ld年%ld月%ld日%ld小時(shí)%ld分鐘%ld秒", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);
NSString * result = nil;
// if (cmps.year && cmps.month && cmps.day && cmps.hour && cmps.minute && cmps.second) {
// result = [NSString stringWithFormat:@"%ld年%ld月%ld日%ld小時(shí)%ld分鐘%ld秒",cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second];
// }else if (cmps.month && cmps.day && cmps.hour && cmps.minute && cmps.second){
// result = [NSString stringWithFormat:@"%ld月%ld日%ld小時(shí)%ld分鐘%ld秒", cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second];
// }else if (cmps.day && cmps.hour && cmps.minute && cmps.second){
// result = [NSString stringWithFormat:@"%ld日%ld小時(shí)%ld分鐘%ld秒", cmps.day, cmps.hour, cmps.minute, cmps.second];
// }else if (cmps.hour && cmps.minute && cmps.second){
// result = [NSString stringWithFormat:@"%ld小時(shí)%ld分鐘%ld秒", cmps.hour, cmps.minute, cmps.second];
// }else if (cmps.minute && cmps.second){
// result = [NSString stringWithFormat:@"%ld分鐘%ld秒", cmps.minute, cmps.second];
// }else if (cmps.second){
// result = [NSString stringWithFormat:@"%ld秒", cmps.second];
// }else{
// result = @"0秒";
// }
if (cmps.year ) {
result = [NSString stringWithFormat:@"%ld年前",(long)cmps.year];
}else if (cmps.month){
result = [NSString stringWithFormat:@"%ld月前", (long)cmps.month];
}else if (cmps.day){
result = [NSString stringWithFormat:@"%ld天前", (long)cmps.day];
}else if (cmps.hour){
result = [NSString stringWithFormat:@"%ld小時(shí)前", (long)cmps.hour];
}else if (cmps.minute){
result = [NSString stringWithFormat:@"%ld分鐘前", (long)cmps.minute];
}else if (cmps.second){
result = [NSString stringWithFormat:@"%ld秒前", (long)cmps.second];
}else{
result = @"0秒";
}
return result;
}
//時(shí)間戳轉(zhuǎn)化為實(shí)踐
+(NSString *)timeStampChangeTime:(NSString *)timeStamp andFormatter:(NSString *)Formatter{
// iOS 生成的時(shí)間戳是10位
NSTimeInterval interval =[timeStamp doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:Formatter];
NSString *dateString = [formatter stringFromDate: date];
return dateString;
}
//秒數(shù)轉(zhuǎn)化時(shí)分
+(NSString *)timeChangeMS:(NSInteger)time{
int seconds = time % 60;
int minutes = time / 60;
return [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
}
@end