首页 > 其他分享 >IOS开发-与时间相关NSCalendar、NSDate类的基本使用

IOS开发-与时间相关NSCalendar、NSDate类的基本使用

时间:2023-07-19 12:22:52浏览次数:47  
标签:ld NSCalendar IOS long NSDate components calendar

1.获取世界统一时间和当前城市时间

-(void)getNowDate{
    NSDate *nowDate = [NSDate date];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    NSInteger ti = [localTimeZone secondsFromGMTForDate:nowDate];
    NSDate *newDate = [nowDate dateByAddingTimeInterval:ti];
    NSLog(@"世界统一时间:%@ 当地时间:%@", nowDate, newDate);

}

 2.获取当前时间的时分秒

-(void)getFormattdate{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

    NSDateComponents *components = [calendar components:unit fromDate:[NSDate date]];

    NSLog(@"年:%ld 月:%ld 日:%ld 时:%ld 分:%ld 秒:%ld",(long)components.year, (long)components.month, (long)components.day, (long)components.hour, (long)components.minute, (long)components.second);
}

3.比较两个时间之间的差值

-(void)compareDate{
    NSDate *date1 = [NSDate date]; // 当前的日期

    NSString *dateStr = @"2015-06-29 07:05:26 +0000";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";
    NSDate *date2 = [formatter dateFromString:dateStr];  // 指定的日期

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *components = [calendar components:unit fromDate:date1 toDate:date2 options:0];

    NSLog(@"相差年:%ld 月:%ld 日:%ld 时:%ld 分:%ld 秒:%ld",(long)components.year, (long)components.month, (long)components.day, (long)components.hour, (long)components.minute, (long)components.second);
}

4.获取当前日期的那天在所在月份是星期几 

-(void)getWeek{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = 2016;
    components.month = 1;
    components.day = 4;
        
    NSDate *date = [calendar dateFromComponents:components];

    NSUInteger index = [calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfMonth forDate:[NSDate date]];
    NSLog(@"--%lu", index);
    //1:代表星期日  2:代表星期一  3:代表星期二  4:代表星期三  5:代表星期四  6:代表星期五  7:代表星期六
}

 

标签:ld,NSCalendar,IOS,long,NSDate,components,calendar
From: https://www.cnblogs.com/SadicZhou/p/17565238.html

相关文章

  • 使用官方工具在 BIOS 中合成 Intel ME 固件
    众所周知,使用Intel平台的电脑在安装驱动的时候都会看到一个"Intel管理引擎",简称ME(ManagementEngine)。很多人以为这是集成在CPU内的一个官方"后门",它到底是不是后门这点我们无从得知,但是它实际上是集成在芯片组中的一个微处理器,运行一个完整的操作系统而非一般外设的固......
  • IOS开发-OC获取当前时间戳
    //获取当前时间戳-(NSString*)getTimeNow{NSDateFormatter*formatter=[[NSDateFormatteralloc]init];[formattersetDateStyle:NSDateFormatterMediumStyle];[formattersetTimeStyle:NSDateFormatterShortStyle];[formattersetDat......
  • IOS开发-OC页面跳转传递参数
    使用OC进行IOS开发页面跳转传递参数的思路:1.在AppDelegate.h中定义一个可变词典2.在AppDelegate.m中初始化该可变词典3.向字典中添加要传递的参数4.在目标页面拿到参数 1.在AppDelegate.h中定义一个可变词典#import<UIKit/UIKit.h>@interfaceAppDelegate:UIRespo......
  • shutdown 命令 新功能 加参数 重启进入BIOS 加参数 重启进入启动选项
    如上图,shutdown/r/fw注释:此命令,运行后,作用:重启进入BIOS......
  • iOS测试包的安装方法
    iOS测试包根据要安装的机器类型可以分为2种:.app模拟器测试包.ipa真机测试包.app模拟器测试包的安装方式方式一:Xcode生成安装包1.Xcode运行项目,生成app包2.将APP包拖到模拟器中方式二:IPA包下载得到安装包1.将ipa包的后缀改成.zip,然后解压2.取出Payload目录下的.app文件......
  • axios封装的请求及拦截统一管理,和之前相比方便添加自定义请求头和超时
    1、api.js文件importaxiosfrom'axios'import{Message}from'element-ui'consttimeout=5000//默认超时constapi=axios.create({baseURL:'',//设置API的基础URLtimeout:timeout,//设置超时时间,单位为毫秒headers:{'Content-......
  • ios 如何给约束添加动画
    项目方案:iOS如何给约束添加动画1.简介在iOS开发中,使用AutoLayout来布局界面是非常常见的做法。而有时候,我们可能需要给约束添加动画效果,以实现一些特殊的界面效果。本项目方案旨在介绍如何使用iOS的动画库以及AutoLayout来给约束添加动画效果。2.实现方案我们可以使用UIVie......
  • ios 加载网络图片
    iOS加载网络图片在iOS开发中,我们经常需要从网络上加载图片并显示到用户界面上。本文将介绍一种简单的方法来加载网络图片,并附带代码示例。使用第三方库SDWebImageSDWebImage是一个广泛使用的第三方库,它提供了一种简单而高效的方式来加载网络图片,并支持图片缓存和缓存管理。......
  • ios uiswitch大小
    iOSUISwitch大小在iOS开发中,UISwitch是一个常用的控件,用于切换开关状态的显示。UISwitch的大小是可以自定义的,本文将介绍如何调整UISwitch的大小,并提供一些代码示例。1.调整UISwitch的大小UISwitch的大小可以通过调整其frame属性来实现。frame属性包含了控件的位置和尺寸信息,......
  • ios ocr文字识别
    iOSOCR文字识别实现流程1.引入OCR库首先,我们需要引入OCR库来实现文字识别功能。在iOS开发中,可以使用TesseractOCR库来进行文字识别。你可以通过CocoaPods来添加TesseractOCR库到你的项目中。在你的Podfile文件中添加以下代码,并执行podinstall命令来安装OCR库。pod'Tessera......