ios時間工具類

//

//? TimeUtil.h

//? wq

//

//? Created by berwin on 13-7-20.

//? Copyright (c) 2013年 Weqia. All rights reserved.

//

#import

@interface TimeUtil : NSObject

+ (NSString*)getTimeStr:(long) createdAt;

///獲取年月日 時分秒? // 格式:? 2020-09-04 13:50:55

+ (NSString*)getFullTimeStr:(longlong)time;

///獲取年月日? 時分? // 格式:? 2020-09-04 13:50

+ (NSString*)getFullTimeStrNoSecond:(long long)time;

///獲取年月日? // 格式:? 2020.09.04

+ (NSString*)getFullTimeStr1:(long long)time;

///獲取年月日? // 格式:? 2020-09-04

+ (NSString*)getFullTimeStr2:(long long)time;

///獲取天,時分秒? // 格式: 02:13:50:55

+ (NSString*)getDayHourMinuteSecondStr:(long long)time;

///獲取 時分秒? ? // 格式: 13:50:55

+ (NSString*)getHourMinuteSecondStr:(long long)time;

///獲取月日? 09月28日

+ (NSString*)getMDStr:(longlong)time;

///獲取月日 09.28

+ (NSString*)getMDStr1:(longlong)time;

///獲取年

+ (NSString*)getYearStr:(longlong)timeformat:(NSString*)format;

///獲取月

+ (NSString*)getMonthStr:(longlong)timeformat:(NSString*)format;

///獲取日

+ (NSString*)getDayStr:(longlong)timeformat:(NSString*)format;

///獲取星期

+ (NSString*)getWeekdayStr:(longlong)timeformat:(NSString*)format;

+(NSDateComponents*) getComponent:(long long)time;

/// 朋友圈時間格式 例:6天前

/// @param time 時間戳

+(NSString*) getTimeStrStyle1:(long long)time;

+(NSString*) getTimeStrStyle2:(long long)time;

+(NSString*) getTimeStrStyle3:(long long)time;

/// dataFormat

+ (NSString*)getDate:(NSDate*)datewithFormat:(NSString*)dataFormat;

+ (NSDate*)getDateWithDateString:(NSString*)date dateFormat:(NSString*)format;

// 默認(rèn)格式時間,聊天用

+ (NSString*)getDefaultDateFormat:(NSDate*)date;

// 獲取消息列表時間格式

+ (NSString*)getMessageDateFormat:(NSDate*)date;

// 聊天時間格式

+ (NSString*)getChatDateFormat:(NSDate*)date;

// 獲取朋友圈時間格式

+ (NSString*)getFriendsCircleDateFormat:(NSDate*)date;

//

+ (NSString*)getTimeStrStyle4:(NSDate *)date;

/**

?*? 2016.03.11轉(zhuǎn)時間戳

?*

?*? @param time 時間

?*

?*? @return 時間戳

?*/

+ (longlong)getTimestamp:(NSString*)time;

//將NSDate類型的時間轉(zhuǎn)換為時間戳(秒),從1970/1/1開始

+(long long)getDateTimeTOSeconds:(NSDate *)datetime;

//將NSDate類型的時間轉(zhuǎn)換為時間戳字符串(秒),從1970/1/1開始

+(NSString *)getDateTimeTOSecondStr:(NSDate *)datetime;

//將NSDate類型的時間轉(zhuǎn)換為時間戳(毫秒),從1970/1/1開始

+(long long)getDateTimeTOMilliSeconds:(NSDate *)datetime;

//將NSDate類型的時間轉(zhuǎn)換為時間戳字符串(毫秒),從1970/1/1開始

+(NSString *)getDateTimeTOMilliSecondStr:(NSDate *)datetime;

/// 獲取當(dāng)前時間戳(秒)? 格式: 1596685586

+(NSString *)getNowTimeTimestamp;

/// 獲取當(dāng)前時間戳(毫秒)? 格式: 1596685586000

+(NSString *)getNowTimeMilliSecondTimestamp;

/// 獲取當(dāng)前時間戳(秒)? 格式: 1596685586

+(long)getNowTime;

/// 獲取當(dāng)前時間戳(毫秒)? 格式: 1596685586000

+(long)getNowTimeMilliSecond;

/// 獲取零點時間戳? 格式: 1596685586

+(long)getTodayZreoTime;

/// 獲取24點時間戳(秒)? 格式: 1596685586

+(long)getToday24OclockTime;

/// 獲取當(dāng)前時間? 格式: 2019-2-14 10:00:30

+(NSString *)getCurrentTimes;

/// 根據(jù)結(jié)束時間來獲取剩余倒計時格式化時間? 格式: 10:00:30

+(NSString *)getHourMinuteSecondCountDownToEndTime:(long)endTime;

/// 根據(jù)結(jié)束時間來獲取剩余倒計時格式化時間? 格式: 10:10:00:30

+(NSString *)getDayHourMinuteSecondCountDownToEndTime:(long)endTime;

/// 根據(jù)結(jié)束時間來獲取剩余倒計時格式化時間? 格式: 10天10時00分30秒

+(NSString *)getDayHourMinuteSecondCountDownToEndTime2:(long)endTime;

/// 根據(jù)指定時間來獲取目標(biāo)天數(shù) 格式: 10天

+(NSString*)getDayToEndTime:(long)startTime;

// 獲取現(xiàn)在時間到N年后的時間

+(NSString*)getNyearsTime:(long)year;

// 獲取指定時間到N年后的時間

+(NSString *)getTargetYearToNyearsTime:(long)targetTime nYears:(long)nYears;

// 傳入秒數(shù)? 得到 xx時xx分

+(NSString*)getMMSSFromSS:(long)totalTime;

@end

//

//? TimeUtil.m

//? wq

//

//? Created by berwin on 13-7-20.

//? Copyright (c) 2013年 Weqia. All rights reserved.

//

#import "TimeUtil.h"

@implementation TimeUtil

+ (NSString*)getTimeStr:(long) createdAt

{

? ? // Calculate distance time string

? ? //

? ? NSString*timestamp;

? ? time_tnow;

? ? time(&now);


? ? intdistance = (int)difftime(now, createdAt);

? ? if(distance <0) distance =0;


? ? if(distance <60) {

? ? ? ? timestamp = [NSStringstringWithFormat:@"%d %s", distance, (distance ==1) ?"second ago":"seconds ago"];

? ? }

? ? elseif(distance <60*60) {

? ? ? ? distance = distance /60;

? ? ? ? timestamp = [NSStringstringWithFormat:@"%d %s", distance, (distance ==1) ?"minute ago":"minutes ago"];

? ? }

? ? elseif(distance <60*60*24) {

? ? ? ? distance = distance /60/60;

? ? ? ? timestamp = [NSStringstringWithFormat:@"%d %s", distance, (distance ==1) ?"hour ago":"hours ago"];

? ? }

? ? elseif(distance <60*60*24*7) {

? ? ? ? distance = distance /60/60/24;

? ? ? ? timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance ==1) ?"day ago":"days ago"];

? ? }

? ? elseif(distance <60*60*24*7*4) {

? ? ? ? distance = distance /60/60/24/7;

? ? ? ? timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance ==1) ?"week ago":"weeks ago"];

? ? }

? ? else{

? ? ? ? staticNSDateFormatter *dateFormatter =nil;

? ? ? ? if(dateFormatter ==nil) {

? ? ? ? ? ? dateFormatter = [[NSDateFormatter alloc] init];

? ? ? ? ? ? [dateFormatter setDateStyle:NSDateFormatterShortStyle];

? ? ? ? ? ? [dateFormatter setTimeStyle:NSDateFormatterShortStyle];

? ? ? ? }


? ? ? ? NSDate *date = [NSDate dateWithTimeIntervalSince1970:createdAt];?

? ? ? ? timestamp = [dateFormatter stringFromDate:date];

? ? }

? ? returntimestamp;

}

+ (NSString*)getFullTimeStr:(longlong)time

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSString * string=[NSString stringWithFormat:@"%04zd-%02zd-%02zd %02zd:%02zd:%02zd",[component year],[component month],[component day],[component hour],[component minute],[component second]];

? ? returnstring;

}

+ (NSString*)getDayHourMinuteSecondStr:(long long)time

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSString * string=[NSString stringWithFormat:@"%02zd:%02zd:%02zd:%02zd",[component day],[component hour],[component minute],[component second]];

? ? returnstring;

}

+ (NSString*)getHourMinuteSecondStr:(long long)time

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSString * string=[NSString stringWithFormat:@"%02zd:%02zd:%02zd",[component hour],[component minute],[component second]];

? ? returnstring;

}

+ (NSString*)getFullTimeStrNoSecond:(long long)time

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSString * string=[NSString stringWithFormat:@"%04zd-%02zd-%02zd %02zd:%02zd",[component year],[component month],[component day],[component hour],[component minute]];

? ? returnstring;

}

+ (NSString*)getFullTimeStr1:(longlong)time

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSString * string=[NSString stringWithFormat:@"%04zd.%02zd.%02zd ",[component year],[component month],[component day]];

? ? returnstring;

}

+ (NSString*)getFullTimeStr2:(longlong)time

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSString * string=[NSString stringWithFormat:@"%04zd-%02zd-%02zd ",[component year],[component month],[component day]];

? ? returnstring;

}

+ (NSString*)getMDStr:(longlong)time

{


? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSString * string=[NSString stringWithFormat:@"%.2zd月%.2zd日",[component month],[component day]];

? ? returnstring;

}

+ (NSString*)getMDStr1:(longlong)time

{


? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSString * string=[NSString stringWithFormat:@"%.2zd-%.2zd",[component month],[component day]];

? ? returnstring;

}

+ (NSString*)getMonthStr:(longlong)timeformat:(NSString *)format

{


? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSString * string =nil;

? ? if(format ==nil) {

? ? ? ? string=[NSString stringWithFormat:@"%.2zd",[component month]];

? ? }else{

? ? ? ? string=[NSString stringWithFormat:@"%.2zd%@",[component month],format];

? ? }

? ? returnstring;

}

+ (NSString*)getYearStr:(longlong)timeformat:(NSString *)format

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSString * string =nil;

? ? if(format ==nil) {

?? ? ? ? string = [NSString stringWithFormat:@"%04zd",[component year]];

? ? }else{

?? ? ? ? string = [NSString stringWithFormat:@"%04zd%@",[component year],format];

? ? }

? ? returnstring;

}

+ (NSString*)getDayStr:(longlong)timeformat:(NSString *)format

{


? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];


? ? NSString * string =nil;

? ? if(format ==nil) {

? ? ? ? string=[NSString stringWithFormat:@"%zd",[component day]];

? ? }else{

? ? ? ? string=[NSString stringWithFormat:@"%zd%@",[component day],format];

? ? }

? ? returnstring;

}

+ (NSString*)getWeekdayStr:(longlong)timeformat:(NSString *)format{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];


? ? NSInteger year=[component year];

? ? NSInteger week=[component weekOfYear];

? ? NSInteger weekday=[component weekday];


? ? NSDate * today=[NSDate date];

? ? component=[calendar components:unitFlags fromDate:today];


? ? NSInteger t_year=[component year];

? ? NSInteger t_week=[component weekOfYear];


? ? NSString*string=nil;

?? if(year==t_year&&week==t_week)

? ? {

? ? ? ? NSString * daystr=nil;

? ? ? ? switch(weekday) {

? ? ? ? ? ? case1:

? ? ? ? ? ? ? ? daystr=@"日";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case2:

? ? ? ? ? ? ? ? daystr=@"一";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case3:

? ? ? ? ? ? ? ? daystr=@"二";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case4:

? ? ? ? ? ? ? ? daystr=@"三";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case5:

? ? ? ? ? ? ? ? daystr=@"四";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case6:

? ? ? ? ? ? ? ? daystr=@"五";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case7:

? ? ? ? ? ? ? ? daystr=@"六";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? default:

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? ? ? if(format ==nil) {

? ? ? ? ? ? string=[NSString stringWithFormat:@"%@",daystr];

? ? ? ? }else{

? ? ? ? ? ? string=[NSString stringWithFormat:@"%@%@",format,daystr];

? ? ? ? }

? ? }

? ? returnstring;

}

+(NSDateComponents*)getComponent:(longlong)time

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? returncomponent;

}

+(NSString*)getTimeStrStyle1:(longlong)time

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];

? ? NSInteger year=[component year];

? ? NSInteger month=[component month];

? ? NSInteger day=[component day];


? ? NSDate * today=[NSDate date];

? ? component=[calendar components:unitFlags fromDate:today];


? ? NSInteger t_year=[component year];


? ? NSString*string=nil;


? ? longlongnow=[today timeIntervalSince1970];


? ? longdistance=(long)(now-time);

? ? if(distance<60)

? ? ? ? string=@"剛剛";

? ? elseif(distance<60*60)

? ? ? ? string=[NSString stringWithFormat:@"%ld 分鐘前",distance/60];

? ? elseif(distance<60*60*24)

? ? ? ? string=[NSString stringWithFormat:@"%ld 小時前",distance/60/60];

? ? elseif(distance<60*60*24*7)

? ? ? ? string=[NSString stringWithFormat:@"%ld 天前",distance/60/60/24];

? ? elseif(year==t_year)

? ? ? ? string=[NSString stringWithFormat:@"%zd月%zd日",month,day];

? ? else

? ? ? ? string=[NSString stringWithFormat:@"%zd年%zd月%zd日",year,month,day];


? ? returnstring;? ?


}

+(NSString*)getTimeStrStyle2:(longlong)time

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];


? ? NSInteger year=[component year];

? ? NSInteger month=[component month];

? ? NSInteger day=[component day];

? ? NSInteger hour=[component hour];

? ? NSInteger minute=[component minute];

? ? NSInteger week=[component weekOfYear];

? ? NSInteger weekday=[component weekday];


? ? NSDate * today=[NSDate date];

? ? component=[calendar components:unitFlags fromDate:today];


? ? NSInteger t_year=[component year];

? ? NSInteger t_month=[component month];

? ? NSInteger t_day=[component day];

? ? NSInteger t_week=[component weekOfYear];


? ? NSString*string=nil;

? ? if(year==t_year&&month==t_month&&day==t_day)

? ? {

? ? ? ? if(hour<6&&hour>=0)

?? ? ? ? ? ? string=[NSString stringWithFormat:@"凌晨 %zd:%02zd",hour,minute];

? ? ? ? elseif(hour>=6&&hour<12)

? ? ? ? ? ? string=[NSString stringWithFormat:@"上午 %zd:%02zd",hour,minute];

? ? ? ? elseif(hour>=12&&hour<18)

? ? ? ? ? ? string=[NSString stringWithFormat:@"下午 %zd:%02zd",hour-12,minute];

? ? ? ? else

? ? ? ? ? ? string=[NSString stringWithFormat:@"晚上 %zd:%02zd",hour-12,minute];

? ? }

? ? elseif(year==t_year&&week==t_week)

? ? {

? ? ? ? NSString * daystr=nil;

? ? ? ? switch(weekday) {

? ? ? ? ? ? case1:

? ? ? ? ? ? ? ? daystr=@"日";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case2:

? ? ? ? ? ? ? ? daystr=@"一";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case3:

? ? ? ? ? ? ? ? daystr=@"二";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case4:

? ? ? ? ? ? ? ? daystr=@"三";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case5:

? ? ? ? ? ? ? ? daystr=@"四";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case6:

? ? ? ? ? ? ? ? daystr=@"五";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case7:

? ? ? ? ? ? ? ? daystr=@"六";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? default:

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? ? ? string=[NSString stringWithFormat:@"周%@ %zd:%02zd",daystr,hour,minute];

? ? }

? ? elseif(year==t_year)

? ? ? ? string=[NSString stringWithFormat:@"%zd月%zd日",month,day];

? ? else

? ? ? ? string=[NSString stringWithFormat:@"%zd年%zd月%zd日",year,month,day];


? ? returnstring;

}

+(NSString*)getTimeStrStyle3:(longlong)time

{

? ? NSDate * date=[NSDate dateWithTimeIntervalSince1970:time];

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday;

? ? NSDateComponents * component=[calendar components:unitFlags fromDate:date];


? ? NSInteger year=[component year];

? ? NSInteger month=[component month];

? ? NSInteger day=[component day];

? ? NSInteger hour=[component hour];

? ? NSInteger minute=[component minute];


? ? NSDate * today=[NSDate date];

? ? component=[calendar components:unitFlags fromDate:today];


? ? NSInteger t_year=[component year];

? ? NSInteger t_month=[component month];

? ? NSInteger t_day=[component day];


? ? NSString*string=nil;

? ? if(year==t_year&&month==t_month&&day==t_day)

? ? {

? ? ? ? if(hour<6&&hour>=0)

? ? ? ? ? ? string=[NSString stringWithFormat:@"凌晨 %zd:%02zd",hour,minute];

? ? ? ? elseif(hour>=6&&hour<12)

? ? ? ? ? ? string=[NSString stringWithFormat:@"上午 %zd:%02zd",hour,minute];

? ? ? ? elseif(hour>=12&&hour<18)

? ? ? ? ? ? string=[NSString stringWithFormat:@"下午 %zd:%02zd",hour-12,minute];

? ? ? ? else

? ? ? ? ? ? string=[NSString stringWithFormat:@"晚上 %zd:%02zd",hour-12,minute];

? ? }

? ? elseif(year==t_year)

? ? ? ? string=[NSString stringWithFormat:@"%zd-%zd %zd:%02zd",month,day,hour,minute];

? ? else

? ? ? ? string=[NSString stringWithFormat:@"%02zd-%zd-%zd %zd:%02zd",year%100,month,day,hour,minute];


? ? returnstring;

}

#pragma mark- dataFormat

+ (NSString*)getDate:(NSDate*)datewithFormat:(NSString*)dataFormat{

? ? if(date ==nil) {

? ? ? ? date = [[NSDate alloc] init];

? ? }

? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

? ? [formatter setDateFormat:dataFormat];

? ? NSString *theDate = [formatter stringFromDate:date];

? ? returntheDate;

}

+ (NSDate*)getDateWithDateString:(NSString*)datedateFormat:(NSString*)format{

? ? NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

? ? [dateFormat setDateFormat:format];

? ? return[dateFormat dateFromString:date];

}

+ (NSString*)getDefaultDateFormat:(NSDate*)date{

? ? NSDate * today = [NSDate date];

? ? NSDate * refDate = date;


? ? NSString * todayString = [[selfgetDate:today withFormat:kDateFormatDefault] substringToIndex:10];

? ? NSString * refDateString = [[selfgetDate:refDate withFormat:kDateFormatDefault] substringToIndex:10];


? ? NSString * yearString = [[selfgetDate:today withFormat:kDateFormatDefault] substringToIndex:4];

? ? NSString * refYearString = [[selfgetDate:refDate withFormat:kDateFormatDefault] substringToIndex:4];


? ? if([refDateString isEqualToString:todayString])

? ? {

? ? ? ? return[selfgetDate:date withFormat:kDateFormatTime];

? ? }elseif([yearString isEqualToString:refYearString]){

? ? ? ? return[selfgetDate:date withFormat:kDateFormat_MdHms];

? ? }else{

? ? ? ? return[selfgetDate:date withFormat:kDateFormatDefault];

? ? }

}

+ (NSString*)getMessageDateFormat:(NSDate*)date{

? ? NSDate * today = [NSDate date];

? ? NSDate * refDate = date;


? ? NSString * todayString = [[selfgetDate:today withFormat:kDateFormatDefault] substringToIndex:10];

? ? NSString * refDateString = [[selfgetDate:refDate withFormat:kDateFormatDefault] substringToIndex:10];


? ? NSString * yearString = [[selfgetDate:today withFormat:kDateFormatDefault] substringToIndex:4];

? ? NSString * refYearString = [[selfgetDate:refDate withFormat:kDateFormatDefault] substringToIndex:4];


? ? if([refDateString isEqualToString:todayString])

? ? {

? ? ? ? return[selfgetDate:date withFormat:kDateFormat_Hm];

? ? }elseif([yearString isEqualToString:refYearString]){

? ? ? ? return[selfgetDate:date withFormat:kDateFormat_Md];

? ? }else{

? ? ? ? return[selfgetDate:date withFormat:kDateFormat_yyMd];

? ? }

}

+ (NSString*)getChatDateFormat:(NSDate*)date{

? ? //獲取系統(tǒng)是24小時制或者12小時制

? ? NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];

? ? NSRangecontainsA = [formatStringForHoursrangeOfString:@"a"];

? ? BOOLhasAMPM = containsA.location!=NSNotFound;

? ? //hasAMPM==TURE為12小時制,否則為24小時制

? ? if(hasAMPM) {

? ? ? ? return [self getTimeStrStyle3:date.timeIntervalSince1970];

? ? }else{

? ? ? ? NSDate* today = [NSDatedate];

? ? ? ? NSDate* refDate = date;


? ? ? ? NSString* todayString = [[selfgetDate:todaywithFormat:kDateFormatDefault]substringToIndex:10];

? ? ? ? NSString* refDateString = [[selfgetDate:refDatewithFormat:kDateFormatDefault]substringToIndex:10];


? ? ? ? NSString* yearString = [[selfgetDate:todaywithFormat:kDateFormatDefault]substringToIndex:4];

? ? ? ? NSString* refYearString = [[selfgetDate:refDatewithFormat:kDateFormatDefault]substringToIndex:4];


? ? ? ? if([refDateStringisEqualToString:todayString])

? ? ? ? {

? ? ? ? ? ? return[selfgetDate:datewithFormat:kDateFormat_Hm];

? ? ? ? }elseif([yearStringisEqualToString:refYearString]){

? ? ? ? ? ? return[selfgetDate:datewithFormat:kDateFormat_MdHm];

? ? ? ? }else{

? ? ? ? ? ? return[selfgetDate:datewithFormat:kDateFormat_yyMdHm];

? ? ? ? }

? ? }

}

+ (NSString*)getFriendsCircleDateFormat:(NSDate*)date{

? ? NSDate* today = [NSDatedate];

? ? NSDate* refDate = date;


? ? NSString* todayString = [[selfgetDate:todaywithFormat:kDateFormatDefault]substringToIndex:10];

? ? NSString* refDateString = [[selfgetDate:refDatewithFormat:kDateFormatDefault]substringToIndex:10];


? ? NSString* yearString = [[selfgetDate:todaywithFormat:kDateFormatDefault]substringToIndex:4];

? ? NSString* refYearString = [[selfgetDate:refDatewithFormat:kDateFormatDefault]substringToIndex:4];


? ? if([refDateStringisEqualToString:todayString])

? ? {

? ? ? ? return@"今天";

? ? }elseif([yearStringisEqualToString:refYearString]){

? ? ? ? return[selfgetDate:datewithFormat:kDateFormat_Md];

? ? }else{

? ? ? ? return[selfgetDate:datewithFormat:kDateFormat_yyMd];

? ? }

}

+ (NSString*)getTimeStrStyle4:(NSDate *)date

{

? ? NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

? ? NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday;

? ? NSDateComponents* component=[calendarcomponents:unitFlagsfromDate:date];


? ? NSIntegeryear=[componentyear];

? ? NSIntegermonth=[componentmonth];

? ? NSIntegerday=[componentday];

? ? NSIntegerhour=[componenthour];

? ? NSIntegerminute=[componentminute];

? ? NSIntegerweekday=[componentweekday];


? ? NSDate* today=[NSDatedate];

? ? component=[calendarcomponents:unitFlagsfromDate:today];

? ? NSIntegert_year=[componentyear];

? ? NSInteger t_month=[component month];

? ? NSInteger t_day=[component day];



? ? NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-86400];


? ? NSString * todayString = [[selfgetDate:date withFormat:kDateFormatDefault] substringToIndex:10];

? ? NSString * refDateString = [[selfgetDate:yesterday withFormat:kDateFormatDefault] substringToIndex:10];


? ? NSString *noTimeStr = [selfgetDate:date withFormat:kDateFormat_yMd];

? ? NSDate *noTime = [selfgetDateWithDateString:noTimeStr dateFormat:kDateFormat_yMd];


? ? longlongnow=[today timeIntervalSince1970];

? ? longdistance=(long)(now-[noTime timeIntervalSince1970]);



? ? NSString*string=[NSString stringWithFormat:@"%zd:%02zd",hour,minute];

? ? if(year==t_year&&month==t_month&&day==t_day){}

? ? elseif([todayString isEqualToString:refDateString])

? ? ? ? string=[NSString stringWithFormat:@"昨天 %zd:%02zd",hour,minute];

? ? elseif(distance<60*60*24*7)

? ? {

? ? ? ? NSString *daystr;

? ? ? ? switch(weekday) {

? ? ? ? ? ? case1:

? ? ? ? ? ? ? ? daystr=@"日";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case2:

? ? ? ? ? ? ? ? daystr=@"一";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case3:

? ? ? ? ? ? ? ? daystr=@"二";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case4:

? ? ? ? ? ? ? ? daystr=@"三";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case5:

? ? ? ? ? ? ? ? daystr=@"四";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case6:

? ? ? ? ? ? ? ? daystr=@"五";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case7:

? ? ? ? ? ? ? ? daystr=@"六";

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? default:

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? ? ? string=[NSString stringWithFormat:@"星期%@ %zd:%02zd",daystr,hour,minute];

? ? }

? ? elseif(year==t_year)

? ? ? ? string=[NSString stringWithFormat:@"%zd-%zd %zd:%02zd",month,day,hour,minute];

? ? else

? ? ? ? string=[NSString stringWithFormat:@"%02zd-%zd-%zd %zd:%02zd",year%100,month,day,hour,minute];


? ? returnstring;

}

/**

?*? 2016.03.11轉(zhuǎn)時間戳

?*

?*? @param time 時間

?*

?*? @return 時間戳

?*/

+ (longlong)getTimestamp:(NSString *)time

{

? ? NSString * year = [time substringWithRange:NSMakeRange(0,4)];

? ? NSString * moth = [time substringWithRange:NSMakeRange(5,2)];

? ? NSString * day = [time substringWithRange:NSMakeRange(8,2)];

? ? NSString *timeStr = [NSString stringWithFormat:@"%@-%@-%@ 00:00:00",year,moth,day];

? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

? ? [formatter setDateStyle:NSDateFormatterMediumStyle];

? ? [formatter setTimeStyle:NSDateFormatterShortStyle];

? ? [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

? ? NSDate* date = [formatter dateFromString:timeStr];//將字符串按formatter轉(zhuǎn)成nsdate

?? ? longlongtimeSp = [date timeIntervalSince1970];

?? ? returntimeSp;

}

//將NSDate類型的時間轉(zhuǎn)換為時間戳(秒),從1970/1/1開始

+(longlong)getDateTimeTOSeconds:(NSDate *)datetime{

? ? return[selfgetDateTimeTOMilliSeconds:datetime]/1000.0;

}

//將NSDate類型的時間轉(zhuǎn)換為時間戳字符串(秒),從1970/1/1開始

+(NSString *)getDateTimeTOSecondStr:(NSDate *)datetime{

? ? return[NSString stringWithFormat:@"%lld",[selfgetDateTimeTOSeconds:datetime]];

}

//將NSDate類型的時間轉(zhuǎn)換為時間戳,從1970/1/1開始

+(long long)getDateTimeTOMilliSeconds:(NSDate *)datetime{


? ? NSTimeInterval interval = [datetime timeIntervalSince1970];

? ? longlongtotalMilliseconds = interval*1000;

? ? returntotalMilliseconds;

}

+(NSString *)getDateTimeTOMilliSecondStr:(NSDate *)datetime{

? ? return[NSString stringWithFormat:@"%lld",[selfgetDateTimeTOMilliSeconds:datetime]];

}

+(NSString *)getNowTimeTimestamp{

? ? return[NSString stringWithFormat:@"%ld", [selfgetNowTime]];

}

+(NSString *)getNowTimeMilliSecondTimestamp{

? ? return[NSString stringWithFormat:@"%ld", [selfgetNowTimeMilliSecond]];

}

/// 獲取當(dāng)前時間戳(秒)? 格式: 1596685586

+(long)getNowTime{

? ? NSDate *datenow = [NSDate date];

? ? return(long)[datenow timeIntervalSince1970];

}

/// 獲取當(dāng)前時間戳(毫秒)? 格式: 1596685586000

+(long)getNowTimeMilliSecond{

? ? return[selfgetNowTime]*1000;

}

/// 獲取零點時間戳? 格式: 1596685586

+(long)getTodayZreoTime{


? ? NSDate *now = [NSDate date];

? ? NSCalendar *cal = [NSCalendar currentCalendar];

? ? unsignedintunitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond;

? ? NSDateComponents *zerocompents = [cal components:unitFlags fromDate:now];

? ? // 轉(zhuǎn)化成0晨0點時間

? ? zerocompents.hour =0;

? ? zerocompents.minute =0;

? ? zerocompents.second =0;

? ? // NSdatecomponents轉(zhuǎn)NSdate類型

? ? NSDate* newdate= [cal dateFromComponents:zerocompents];

? ? // //時間轉(zhuǎn)時間戳格式(該時間已經(jīng)是當(dāng)天凌晨零點的時刻)

? ? NSTimeInterval zeroInterval = [newdate timeIntervalSince1970];

? ? return(long)zeroInterval;


}

/// 獲取24點時間戳(秒)? 格式: 1596685586

+(long)getToday24OclockTime{

? ? // 計算二十四點時間戳

? ? longtimeZreoInt = [selfgetTodayZreoTime];

? ? returntimeZreoInt +86400;

}

+ (NSString *)getCurrentTimes{

? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

? ? [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

? ? NSDate *datenow = [NSDate date];

? ? NSString *currentTimeString = [formatter stringFromDate:datenow];

? ? returncurrentTimeString;

}

//根據(jù)結(jié)束時間來獲取剩余倒計時格式化時間? 格式: 10:00:30

+(NSString *)getHourMinuteSecondCountDownToEndTime:(long)endTime{


? ? longnowTime = [selfgetNowTime];

? ? if(nowTime >= endTime) {return@"00:00:00";}

? ? longsecondsCountDown = endTime - nowTime;

? ? // 重新計算 時/分/秒

? ? NSString *hourStr = [NSString stringWithFormat:@"%02ld", secondsCountDown /3600];

? ? NSString *minuteStr = [NSString stringWithFormat:@"%02ld", (secondsCountDown %3600) /60];

? ? NSString *secondStr = [NSString stringWithFormat:@"%02ld", secondsCountDown %60];

? ? NSString *formatTime = [NSString stringWithFormat:@"%@:%@:%@", hourStr, minuteStr, secondStr];

? ? returnformatTime;


}

//根據(jù)結(jié)束時間來獲取剩余倒計時格式化時間? 格式: 10:10:00:30

+(NSString *)getDayHourMinuteSecondCountDownToEndTime:(long)endTime{


? ? longnowTime = [selfgetNowTime];

? ? if(nowTime >= endTime) {return@"00:00:00:00";}

? ? longsecondsCountDown = endTime - nowTime;


? ? // 重新計算 天/時/分/秒

? ? longdaySecond =3600*24;

? ? NSString *dayStr = [NSString stringWithFormat:@"%02ld", secondsCountDown / daySecond];

? ? NSString *hourStr = [NSString stringWithFormat:@"%02ld", (secondsCountDown % daySecond)/3600];

? ? NSString *minuteStr = [NSString stringWithFormat:@"%02ld", (secondsCountDown %3600) /60];

? ? NSString *secondStr = [NSString stringWithFormat:@"%02ld", secondsCountDown %60];

? ? NSString *formatTime = [NSString stringWithFormat:@"%@:%@:%@:%@", dayStr, hourStr, minuteStr, secondStr];

? ? returnformatTime;


}

//根據(jù)結(jié)束時間來獲取剩余倒計時格式化時間? 格式: 10天10時00分30秒

+ (NSString *)getDayHourMinuteSecondCountDownToEndTime2:(long)endTime {

? ? longnowTime = [selfgetNowTime];

? ? if(nowTime >= endTime) {return@"00天00時00分00秒";}

? ? longsecondsCountDown = endTime - nowTime;


? ? // 重新計算 天/時/分/秒

? ? longdaySecond =3600*24;

? ? NSString *dayStr = [NSString stringWithFormat:@"%02ld", secondsCountDown / daySecond];

? ? NSString *hourStr = [NSString stringWithFormat:@"%02ld", (secondsCountDown % daySecond)/3600];

? ? NSString *minuteStr = [NSString stringWithFormat:@"%02ld", (secondsCountDown %3600) /60];

? ? NSString *secondStr = [NSString stringWithFormat:@"%02ld", secondsCountDown %60];

? ? NSString *formatTime = [NSString stringWithFormat:@"%@天%@時%@分%@秒", dayStr, hourStr, minuteStr, secondStr];

? ? returnformatTime;

}

+ (NSString *)getDayToEndTime:(long)startTime {


? ? longnowTime = [selfgetNowTime];

? ? if(nowTime <= startTime) {return@"0天";}

? ? longsecondsCountDown = nowTime - startTime;


? ? // 重新計算 天

? ? longdaySecond =3600*24;

? ? NSString *dayStr = [NSString stringWithFormat:@"%02ld", secondsCountDown / daySecond];

? ? NSString *formatTime = [NSString stringWithFormat:@"%@天", dayStr];

? ? returnformatTime;

}

+ (NSString *)getNyearsTime:(long)year {


? ? longnowTime = [selfgetNowTime];


? ? longtargetTime =60*60*24*365* year;


? ? NSString *str = [selfgetFullTimeStr2:targetTime + nowTime];


? ? returnstr;


}

+ (NSString *)getTargetYearToNyearsTime:(long)targetTimenYears:(long)nYears {


? ? longtargetTime2 =60*60*24*365* nYears;

? ? NSString *str = [selfgetFullTimeStr2:targetTime + targetTime2];


? ? returnstr;


}

+ (NSString *)getMMSSFromSS:(long)totalTime {

? ? NSString *str_hour = [NSString stringWithFormat:@"%02ld",totalTime/3600];

? ? NSString *str_minute = [NSString stringWithFormat:@"%02ld",(totalTime%3600)/60];

? ? NSString *format_time = [NSString stringWithFormat:@"%@時%@分",str_hour,str_minute];


? ? if([format_time isEqualToString:@"00時00分"]) {

? ? ? ? format_time =@"0分";

? ? }

? ? returnformat_time;

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容