日历 NSCalendar
2016-01-08 00:00
267 查看
摘要: 这份代码很简单介绍而来NSCalendar的大概用法,界面只有sb拖出来的button,点击会打印相应的日期。#import "ViewController.h"@interface ViewController ()@property (strong,nonatomic) NSCalendar *calender1;@property (strong,nonatomic) NSCalendar *calender2;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (IBAction)tapBtn1:(id)sender{self.calender1 = [NSCalendar currentCalendar];self.calender2 = [NSCalendar autoupdatingCurrentCalendar];NSLog(@"%@",self.calender1.calendarIdentifier);NSLog(@"%@",self.calender2.calendarIdentifier);}- (IBAction)tapBtn2:(id)sender{NSLog(@"%@",self.calender1.calendarIdentifier);NSLog(@"%@",self.calender2.calendarIdentifier);}//根据提供的日历标示符初始化- (IBAction)tapBtn3:(id)sender{NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];NSLog(@"%@",calendar.calendarIdentifier);}//设置本地化信息- (IBAction)tapBtn4:(id)sender{NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];[calendar setLocale:locale];NSLog(@"%@",calendar.locale.localeIdentifier);}//设置时区信息- (IBAction)tapBtn5:(id)sender{NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"HKT"]];NSLog(@"%@",calendar.timeZone);}//设置每周的第一天从星期几开始- (IBAction)tapBtn6:(id)sender{NSCalendar *calendar = [NSCalendar currentCalendar];[calendar setFirstWeekday:3];NSLog(@"%lu",(unsigned long)calendar.firstWeekday);}//NSCalendar对象的 - (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)date//取得一个NSDate对象的1个或多个部分,用NSDateComponents来封装//需要注意的是,只有明确指定了unitFlags,NSDateComponents相应的那一部分才有值。- (IBAction)tapBtn7:(id)sender{NSCalendar *calendar = [NSCalendar currentCalendar];NSDate *date = [NSDate date];NSDateComponents *compt = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:date];NSLog(@"%ld,%@",(long)[compt year],date);NSLog(@"%ld,%@",(long)[compt month],date);NSLog(@"%ld,%@",(long)[compt day],date);}//NSCalendar对象的 - (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options : (NSUInteger)opts////取得两个NSDate对象的时间间隔,用NSDateComponents来封装- (IBAction)tapBtn8:(id)sender{NSCalendar *calendar = [NSCalendar currentCalendar];NSDate *date = [NSDate date];NSDate *date2 = [NSDate dateWithTimeInterval:5*60*60+75 sinceDate:date];NSDateComponents *compt = [calendar components:(NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date toDate:date2 options:0];NSLog(@"%ld",(long)[compt minute]);NSLog(@"%ld",(long)[compt second]);}//NSCalendar对象的 - (NSDate *)dateFromComponents:(NSDateComponents *)comps//根据NSDateComponents对象得到一个NSDate对象- (IBAction)tapBtn9:(id)sender{NSDateComponents *compt = [[NSDateComponents alloc] init];[compt setYear:2012];[compt setMonth:5];[compt setDay:11];NSCalendar *calendar = [NSCalendar currentCalendar];NSDate *date = [calendar dateFromComponents:compt];//得到本地时间,避免时区问题//获取当前时区NSTimeZone *zone = [NSTimeZone systemTimeZone];//以秒为单位返回当前应用程序与世界标准时间(格林尼治时间)的时差NSInteger interval = [zone secondsFromGMTForDate:date];//补充时差后为当前时间NSDate *localeDate = [date dateByAddingTimeInterval:interval];NSLog(@"%ld",(long)interval);NSLog(@"%@",localeDate);}@end
相关文章推荐
- jQuery支持添加事件的日历特效代码分享(3种样式)
- php简单日历函数
- 简单JavaScript日历及详细说明
- 兼容FireFox 的 js 日历 支持时间的获取
- javascript实现的淘宝旅行通用日历组件用法实例
- Jquery日历插件制作简单日历
- jQuery简单实现日历的方法
- C#简单输出日历的方法
- 一个用JSP做的日历
- 用 php 编写的日历
- C#实现功能强大的中国农历日历操作类
- c语言实现一个简单日历
- 基于jquery实现日历签到功能
- 分享3个php获取日历的函数
- php实现的日历程序
- 教大家制作简单的php日历
- android 开发教程之日历项目实践(三)
- android 开发教程之日历项目实践(二)
- android 开发教程之日历项目实践(一)
- C语言编写多功能日历