在开发的过程中,会遇到一些关于时间的处理,在这里总结一下。
一、获取当前时间
+(NSString *)getCurrentTime{
//获取当前时间,日期
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM"];//此处可以改成你需要的时间格式
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
return currentDateString;
}
二、获取当前时间戳
-(NSString *)getNowTimeTimestamp{
NSDate *dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSLog(@"时间==%d",(int)a);
NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型;
return timeString;
}
三、将UTC日期字符串转为本地时间字符串
-后台返回的时间格式-2018-06-13T15:26:31Z
+(NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//输入格式
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:localTimeZone];
NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
//输出格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
return dateString;
}
四、将本地日期字符串转为UTC日期字符串
本地日期格式:2013-08-03 12:53:51
+ (NSString *)getUTCFormateLocalDate:(NSString *)localDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//输入格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
//输出格式
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
return dateString;
}
五、
时间解析:后台给的格式为yyyy-MM-dd HH:mm:ss.SSS
转换成XXX之前
+ (NSString *)compareCurrentTime:(NSString *)str
{
//把字符串转为NSdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *timeDate = [dateFormatter dateFromString:str];
//得到与当前时间差
NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];
timeInterval = -timeInterval;
//标准时间和北京时间差8个小时
timeInterval = timeInterval - 8*60*60;
long temp = 0;
NSString *result;
if (timeInterval < 60) {
result = [NSString stringWithFormat:@"刚刚"];
}
else if((temp = timeInterval/60) <60){
result = [NSString stringWithFormat:@"%ld分钟前",temp];
}
else if((temp = temp/60) <24){
result = [NSString stringWithFormat:@"%ld小时前",temp];
}
else if((temp = temp/24) <30){
result = [NSString stringWithFormat:@"%ld天前",temp];
}
else if((temp = temp/30) <12){
result = [NSString stringWithFormat:@"%ld月前",temp];
}
else{
temp = temp/12;
result = [NSString stringWithFormat:@"%ld年前",temp];
}
return result;
}
六、时间解析:后台给的为时间戳
转换成XXX之前
+(NSString *)timeFromTimestamp:(NSInteger)timestamp{
NSDateFormatter *dateFormtter =[[NSDateFormatter alloc] init];
NSDate *d = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSTimeInterval late=[d timeIntervalSince1970]*1; //转记录的时间戳
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval now=[dat timeIntervalSince1970]*1; //获取当前时间戳
NSString *timeString=@"";
NSTimeInterval cha=now-late;
// 发表在一小时之内
if (cha/3600<1) {
if (cha/60<1) {
timeString = @"1";
}
else
{
timeString = [NSString stringWithFormat:@"%f", cha/60];
timeString = [timeString substringToIndex:timeString.length-7];
}
timeString=[NSString stringWithFormat:@"%@分钟前", timeString];
}
// 在一小时以上24小以内
else if (cha/3600>1&&cha/86400<1) {
timeString = [NSString stringWithFormat:@"%f", cha/3600];
timeString = [timeString substringToIndex:timeString.length-7];
timeString=[NSString stringWithFormat:@"%@小时前", timeString];
}
// 发表在24以上10天以内
else if (cha/86400>1&&cha/86400*3<1) //86400 = 60(分)*60(秒)*24(小时) 3天内
{
timeString = [NSString stringWithFormat:@"%f", cha/86400];
timeString = [timeString substringToIndex:timeString.length-7];
timeString=[NSString stringWithFormat:@"%@天前", timeString];
}
// 发表时间大于10天
else
{
[dateFormtter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
timeString = [dateFormtter stringFromDate:d];
}
return timeString;
}
七、判读是不是今天以前的日期
+(BOOL)isTodayBefore:(NSString *)year month:(NSString *)month day:(NSString *)day{
//选择的结束日期
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString* beginStr = [NSString stringWithFormat:@"%@-%@-%@ 00:00:00",year,month,day];
NSDate *beginDate=[formatter dateFromString:beginStr];
NSTimeInterval endTime= [beginDate timeIntervalSince1970]; //转换生时间戳
//今天的时间日期
NSLog(@"转换成时间搓====%f",endTime);
NSDate *datenow = [NSDate date];
NSTimeInterval nowTime = [datenow timeIntervalSince1970];
if (endTime>nowTime) {
return YES;
}else{
return NO;
}
}
网友评论