• iOS 时间类经常用法


        //当前日前日期

        NSDate *today = [NSDate date];


        //时区

        NSTimeZone *zone = [NSTimeZone systemTimeZone];


        //设置间隔

        _interval = [zone secondsFromGMTForDate: today];


        //今天+8小时(中国上海时区)

        today = [today  dateByAddingTimeInterval: _interval];


        //获取明天日期

        NSDate *tomorrowDate = [[NSDate alloc]initWithTimeInterval:24 * 60 * 60 sinceDate:[NSDate date]];


        //日历 

       NSCalendar *calendar = [NSCalendar currentCalendar];


       //comps里能够分离出时间年月日等信息

    NSDateComponents *comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit | NSHourCalendarUnit) fromDate:tomorrowDate];


       //获取当前星期几   

        NSInteger weekday = [comps weekday];


      //将NSString类型转换成NSDate    

        NSDate *serverDate = [dateFomatter dateFromString:serverTimeStr];

        NSDate *createDate = [dateFomatter dateFromString:createTimeStr];

     

        //算出两个Date的间隔

       NSTimeInterval betweenTime = [serverDate timeIntervalSinceDate:createDate];


       //分离出标准格式里的年月日时分秒  

        NSDateFormatter *dateFomatter = [[NSDateFormatter alloc]init];

        [dateFomatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];


        //将string换成formatter形式

        NSDate *stringDate = [dateFomatter dateFromString:string];


        //formatter为月和日

        [dateFomatter setDateFormat:@"MM-dd"];


        //提取字符串中月和日

        NSString *str1 = [dateFomatter stringFromDate:stringDate];



       
  • 相关阅读:
    096_mulesoft with salesforce _01
    c++ 内存分区模型 引用 函数提高
    c++ 基础入门
    重点算法题
    密码常识测试
    【dubbo3.x trace组件分享】
    国产操作系统deepin安装与配置NodeRED环境
    pytest框架简介
    pytest添加日志
    斯特林数
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6957840.html
Copyright © 2020-2023  润新知