美文网首页
iOS 比较两个时间的大小,精确到年月日时分秒

iOS 比较两个时间的大小,精确到年月日时分秒

作者: 此时_此景 | 来源:发表于2017-11-16 09:15 被阅读0次

开发中遇到了要从服务器请求时间,和model里的时间比较,刚开始就是拿着系统方法直接比较发现,不能精确到秒,要求是过完凌晨12点算一天。俩个时间必须统一格式,否则,判断不出。时间又分为国际时间和本地时间只说,真是不用不知道,一用发现还有很多区别,所以写下这篇文章,供以后查阅。

时间格式:@"2017-09-27 11:30:15"entity.PublishTime

俩个时间要统一格式,后面的判断可以根据自己的需求改动,我这里要精确到相差到11小时35分,判断写的很垃圾,但是能看懂。

//国际UTC转本地时间

- (NSDate*)getNowDateFromatAnDate:(NSDate*)anyDate {

//设置源日期时区

NSTimeZone* sourceTimeZone = [NSTimeZonetimeZoneWithAbbreviation:@"UTC"];//或GMT

//设置转换后的目标日期时区

NSTimeZone* destinationTimeZone = [NSTimeZonelocalTimeZone];

//得到源日期与世界标准时间的偏移量

NSIntegersourceGMTOffset = [sourceTimeZonesecondsFromGMTForDate:anyDate];

//目标日期与本地时区的偏移量

NSIntegerdestinationGMTOffset = [destinationTimeZonesecondsFromGMTForDate:anyDate];

//得到时间偏移量的差值

NSTimeIntervalinterval = destinationGMTOffset - sourceGMTOffset;

//转为现在时间

NSDate* destinationDateNow = [[NSDatealloc]initWithTimeInterval:intervalsinceDate:anyDate];

returndestinationDateNow;

}

//比较两时间的前后

- (int)compareOneDay:(NSDate*)oneDay withAnotherDay:(NSDate*)anotherDay{

NSCalendar*gregorian =[[NSCalendaralloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

unsignedintunitFlags=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|kCFCalendarUnitHour|kCFCalendarUnitMinute|kCFCalendarUnitSecond;

NSDateComponents*comps=[gregoriancomponents:unitFlagsfromDate:oneDaytoDate:anotherDayoptions:0];

NSIntegeryear= [compsyear];

NSIntegermonth = [compsmonth];

NSIntegerday= [compsday];

NSIntegerhour= [compshour];

NSIntegermin= [compsminute];

NSIntegersecond = [compssecond];

if(year>0) {

    return0;

}

if(month>0&&year<=0) {

       return0;

}

if(month<=0&&year<=0&&day>0) {

       return0;

}

if(day<=0||month<=0||year<=0) {

     if(day<0||month<0||year<0) {

                 return1;

}

if(hour<-11) {

       return1;

}else if(hour>-11) {

       return0;

}else  if(hour==-11) {

if(min>-35) {

    return0;

}else if(min<-35){

    return1;

}else{

if(second>=0) {

   return0;

}else{

   return1;

}

}

}else{

   return0;

}

}else{

    return0;

}

}

相关文章

  • iOS 比较两个时间的大小,精确到年月日时分秒

    开发中遇到了要从服务器请求时间,和model里的时间比较,刚开始就是拿着系统方法直接比较发现,不能精确到秒,要求是...

  • Spring jpa 日期类型精确设置

    TemporalType.TIMESTAMP 精确到秒 年月日时分秒 TemporalType.DATE 精确到天...

  • mybatis中时间比较Date和String

    在常规操作中,mysql中存在精确到年月日时分秒、年月日、年月、年的查询或者比较 mybatis中如何去实现这样:...

  • 时间工具类总结

    默认: 1、比较年月日时分秒的字符串的大小20180315100900 2.获取当前毫秒值时间 3. 4.

  • 系统时间相关-date

    1.查看系统时间 年月日 完整显示年月日 完整显示年月日 时分秒 时分秒 星期 显示过去或未来的时间信息: 同步系...

  • datetime:时间戳转换

    将时间戳转换为年月日时分秒格式: 将年月日时分秒格式转换为时间戳:

  • android获取两个时间段的分钟差

    /** * 获取两个时间段的分钟差 * * @param startDate 年月日时分秒 * @...

  • day19-time模块

    1. 时间模块 主要包含处理年月日时分秒对应的时间(着重时分秒)import time 专门处理年月日import...

  • day19-time模块

    01-时间模块 主要包含处理年月日时分秒对应的时间(着重时分秒)import time 专门处理年月日import...

  • Day19-课堂笔记-time

    主要包含处理年月日时分秒对应的时间(着重时分秒)import time专门处理年月日import datetime...

网友评论

      本文标题:iOS 比较两个时间的大小,精确到年月日时分秒

      本文链接:https://www.haomeiwen.com/subject/negkvxtx.html