• 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];



       
  • 相关阅读:
    BeautifulSoup
    requests
    安装xpath helper
    取消搜狗输入法的快捷键
    numpy初识 old
    Jupyter Notebook 快捷键
    安装numpy、matplotlib
    JavaScript 继承 -JavaScript高级程序设计
    mac /windows
    unicode 地址
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6957840.html
Copyright © 2020-2023  润新知