標(biāo)準(zhǔn)時(shí)間即(2017-05-05 17:33:09)字符串;
//時(shí)間戳 轉(zhuǎn) 標(biāo)準(zhǔn)時(shí)間(2017-07-15格式) - wsx注釋
+(NSString *)timeStampSwitchStandardTime:(long long)timeStamp{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];//秒數(shù)@"1493976789"轉(zhuǎn)時(shí)間
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *time = [formatter stringFromDate:date];//時(shí)間轉(zhuǎn)標(biāo)準(zhǔn)時(shí)間字符串
NSLog(@"標(biāo)準(zhǔn)時(shí)間字符串:%@",time);
return time;
}
打印截圖:

秒轉(zhuǎn)標(biāo)準(zhǔn)時(shí)間.png
進(jìn)階轉(zhuǎn)換,顯示多少秒/分/小時(shí)/天之前,代碼如下:
//時(shí)間戳 轉(zhuǎn) 標(biāo)準(zhǔn)時(shí)間(2017-07-15格式) - wsx注釋
+(NSString *)timeStampSwitchStandardTime:(long long)timeStamp{
NSDate *nowDate = [NSDate date];
NSDate *oldDate = [NSDate dateWithTimeIntervalSince1970:timeStamp];//時(shí)間戳轉(zhuǎn)Date時(shí)間
// 兩個(gè)時(shí)間戳的間隔 返回的是秒
NSTimeInterval timeInterval = [nowDate timeIntervalSinceDate:oldDate];
NSInteger interval = (NSInteger)timeInterval;
//日期獲取 - wsx注釋
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd"];
NSString *time;
if (interval < 60 ) {
time = [NSString stringWithFormat:@"%ld秒前",(long)interval];
}else if (interval < 60*60){
time = [NSString stringWithFormat:@"%ld分前",(long)interval/60];
}else if (interval < 60*60*24){
time = [NSString stringWithFormat:@"%ld小時(shí)前",(long)interval/3600];
}else if (interval < 60*60*24*2){
time = @"昨天";
}else if (interval < 60*60*24*3){
time = @"前天";
}else{
time = [formatter stringFromDate:oldDate];//時(shí)間轉(zhuǎn)標(biāo)準(zhǔn)時(shí)間字符串
}
NSLog(@"標(biāo)準(zhǔn)時(shí)間字符串:%@",time);
return time;
}
標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)時(shí)間戳:
- (NSString *)obtainTimeStampFromStandardTimeWithStartTime:(NSString *)startTime EndTime:(NSString *)endTime {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // hh表示:12小時(shí)制,HH表示:24小時(shí)制
//設(shè)置時(shí)區(qū),這個(gè)對(duì)于時(shí)間的處理很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[dateFormatter setTimeZone:timeZone];
NSString *nowTimeStamp = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
// 開(kāi)始時(shí)間轉(zhuǎn)換
NSDate *startDate = [dateFormatter dateFromString:startTime]; // 將standardTime字符串按dateFormatter轉(zhuǎn)成NSDate
NSString *startTimeStamp = [NSString stringWithFormat:@"%ld", (long)[startDate timeIntervalSince1970]]; // date與當(dāng)前時(shí)間差8小時(shí) // 得到秒
// 結(jié)束時(shí)間轉(zhuǎn)換
NSDate *endDate = [dateFormatter dateFromString:endTime];
NSString *endTimeStamp = [NSString stringWithFormat:@"%ld", (long)[endDate timeIntervalSince1970]]; // 得到秒
// 比較時(shí)間差
if ([[NSDate date] timeIntervalSinceDate:startDate] < 0.0f) {
return @"未開(kāi)始";
} else if ([[NSDate date] timeIntervalSinceDate:endDate] > 0.0f) {
return @"已結(jié)束";
}
return @"進(jìn)行中";
}