OC_NSDate

来源:互联网 发布:七政四余择日软件 编辑:程序博客网 时间:2024/06/09 21:56

今天记录NSDate时间类,通过这个类可以获得设备当前的时间,并使用适合的方式变为字符串来使用!

1、获得当前一个时间NSDate *date = [NSDate date];//获取本地系统当前时间2、得到当前时间 时隔(参数)之后的某个时间,如果往前算用负号(-)    NSDate *tomorrowDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60];3、格式化时间,准备一个操作时间格式化的对象,这个对象用来设置我们如何格式化一个时间    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    3.1 系统给定格式的格式化(日期格式化、时间格式化),把当前时间转为字符串,根据设置的格式来打印    [dateFormatter setDateS<span id="transmark"></span>tyle:NSDateFormatterLongStyle];    [dateFormatter setTimeStyle:NSDateFormatterLongStyle];    NSLog(@"%@",[dateFormatter stringFromDate:date]);    3.2 自定义日期和时间格式      //yyyy是年,MM是月,dd是日,hh是小时,mm是分钟,ss是秒,SSS是毫秒    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];4、计算两个日期的时间间隔,两个日期,返回值是一个timeInterval的时间    NSTimeInterval timeInterval = [tenHourDate timeIntervalSinceDate:date];


5、日历

1日历 的对象

2、取出日历中 元素对象

3、设置日历中能用到的元素的集合,(选择你想要用的那些元素)

4、第一个参数就是上面做的元素对象的集合

5、把元素和时间合起来生成一个集合,用的时候取出一个基本类型的日期

NSCalendar*calendar=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];NSDateComponents *com = [[NSDateComponents alloc] init];NSInteger flag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit | NSWeekdayCalendarUnit;com = [calendar components:flag fromDate:date]; int year = [com year]; int month = [com month]; int day = [com day]; int hour = [com hour]; int minute = [com minute]; int second = [com second]; int week = [com weekday];


0 0