当前位置: 首页 > news >正文

网站主页设计素材广东英德网站建设

网站主页设计素材,广东英德网站建设,高级软件开发工程师证书含金量,商城类网站方案主要有以下类#xff1a; NSDate -- 表示一个绝对的时间点NSTimeZone -- 时区信息NSLocale -- 本地化信息NSDateComponents -- 一个封装了具体年月日、时秒分、周、季度等的类NSCalendar -- 日历类#xff0c;它提供了大部分的日期计算接口#xff0c;并且允许您在NSDate和N… 主要有以下类 NSDate -- 表示一个绝对的时间点NSTimeZone -- 时区信息NSLocale -- 本地化信息NSDateComponents -- 一个封装了具体年月日、时秒分、周、季度等的类NSCalendar -- 日历类它提供了大部分的日期计算接口并且允许您在NSDate和NSDateComponents之间转换NSDateFormatter -- 用来在日期和字符串之间转换 NSDate NSDate用来表示公历的GMT时间(格林威治时间)。 有下面几种初始化方法 1. - (id)init 默认初始化返回当前时间也可以直接调用类方法 (id)date NSDate *date [[NSDate alloc] init]; //NSDate *date [NSDate date]; NSLog(print date is %,date); 将打印出计算机当前时间2013-03-04 08:57:20 0000 2. - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)seconds 以当前时间的偏移秒数来初始化也可以直接调用类方法  (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)seconds NSDate *date [[NSDate alloc] initWithTimeIntervalSinceNow:20]; //NSDate *date [NSDate dateWithTimeIntervalSinceNow:20]; NSLog(print date is %,date); 假如当前时间是2013-03-04 08:57:20 0000那么初始化后得到的时间是2013-03-04 08:57:40 0000 3. - (id)initWithTimeIntervalSince1970:(NSTimeInterval)seconds 以GMT时间的偏移秒数来初始化也可以直接调用类方法  (id)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds NSDate *date [[NSDate alloc] initWithTimeIntervalSince1970:-20]; //NSDate *date [NSDate dateWithTimeIntervalSince1970:-20]; NSLog(print date is %,date); 得到的时间是格林威治时间往前20秒将打印出1969-12-31 23:59:40 0000 4. - (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds 以2001-1-1 0:0:0的偏移秒数来初始化也可以直接调用类方法  (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds NSDate *date [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:80]; //NSDate *date [NSDate dateWithTimeIntervalSinceReferenceDate:80]; NSLog(print date is %,date); 将打印出2001-01-01 00:01:20 0000 5. - (id)initWithTimeInterval:(NSTimeInterval)seconds sinceDate:(NSDate *)refDate 以基准时间的偏移秒数来初始化也可以直接调用类方法  (id)dateWithTimeInterval:(NSTimeInterval)seconds sinceDate:(NSDate *)date NSDate *date1 [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:20]; NSLog(print date1 is %,date1);NSDate *date2 [[NSDate alloc] initWithTimeInterval:10 sinceDate:date1]; //NSDate *date2 [NSDate dateWithTimeInterval:10 sinceDate:date1]; NSLog(print date2 is %,date2); 第一个基准时间是2001-01-01 00:00:20 0000根据基准时间偏移10秒的结果是2001-01-01 00:00:30 0000 6.   (id)distantPast 与  (id)distantFuture 这两个是类方法分别用来返回一个极早的时间点和一个极晚的时间点 NSDate *date [NSDate distantFuture]; NSLog(future date is %,date);NSDate *date2 [NSDate distantPast]; NSLog(past date is %,date2); distantPast将返回0001-12-30 00:00:00 0000distantFuture将返回4001-01-01 00:00:00 0000 NSDate的常用对象方法 1. -(id)dateByAddingTimeInterval:(NSTimeInterval)seconds 返回以当前NSDate对象为基准偏移多少秒后得到的新NSDate对象。(旧方法 - (id)addTimeInterval:(NSTimeInterval)seconds已被弃用) NSDate *date [NSDate dateWithTimeIntervalSince1970:0]; NSDate *date2 [date dateByAddingTimeInterval:-20]; NSLog(%,date2); 2. - (BOOL)isEqualToDate:(NSDate *)anotherDate 将当前对象与参数传递的对象进行比较根据是否相同返回BOOL值 NSDate *date [NSDate dateWithTimeIntervalSince1970:0]; NSDate *date2 [NSDate dateWithTimeInterval:0 sinceDate:date]; BOOL isEqual [date isEqualToDate:date2]; NSLog(%i,isEqual); 3. - (NSDate *)earlierDate:(NSDate *)anotherDate 与 - (NSDate *)laterDate:(NSDate *)anotherDate 比较两个NSDate对象返回较早/较晚的时间点并以新NSDate对象的形式返回 NSDate *date [NSDate dateWithTimeIntervalSince1970:0]; NSDate *date2 [NSDate dateWithTimeInterval:-50 sinceDate:date];NSDate *date3 [date earlierDate:date2]; NSLog(earlier date is %,date3);NSDate *date4 [date laterDate:date2]; NSLog(later date is %,date4); 4. - (NSComparisonResult)compare:(NSDate *)anotherDate 将当前对象与参数传递的对象进行比较如果相同返回0(NSOrderedSame)对象时间早于参数时间返回-1(NSOrderedAscending)对象时间晚于参数时间返回1(NSOrderedDescending) NSDate *date [NSDate dateWithTimeIntervalSince1970:0]; NSDate *date2 [NSDate dateWithTimeInterval:-50 sinceDate:date];NSInteger result [date compare:date2]; NSLog(%i,result); 5. - (NSTimeInterval)timeIntervalSince1970 返回当前对象时间与1970-1-1 0:0:0的相隔秒数也可以这样理解从1970-1-1 0:0:0开始经过多少秒到达对象指定时间。 NSDate *date [NSDate dateWithTimeIntervalSince1970:50]; NSInteger seconds [date timeIntervalSince1970]; NSLog(%i,seconds); 将返回结果50 6. - (NSTimeInterval)timeIntervalSinceReferenceDate 返回当前对象时间与2001-1-1 0:0:0的相隔秒数也可以这样理解从2001-1-1 0:0:0开始经过多少秒到达对象指定时间。 NSDate *date [NSDate dateWithTimeIntervalSinceReferenceDate:-30]; NSInteger seconds [date timeIntervalSinceReferenceDate]; NSLog(%i,seconds); 将返回结果-30负数代表从2001-1-1 0:0:0开始倒退30秒到达当前时间。 7. - (NSTimeInterval)timeIntervalSinceNow 返回当前对象时间与客户端时间的相隔秒数也可以这样理解从客户端当前时间开始经过多少秒到达对象指定时间。 NSDate *date [NSDate dateWithTimeIntervalSinceNow:100]; NSInteger seconds [date timeIntervalSinceNow]; NSLog(%i,seconds); 经测试返回了结果99但初始化时提供的参数是100。这可能是因为第一句初始化代码到第二句计算代码之间有个1秒内的延时所以计算时的客户端时间比初始化时的客户端时间快了1秒。 8. - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate 返回当前对象时间与参数传递的对象时间的相隔秒数也可以这样理解从参数时间开始经过多少秒到达对象执行时间。 NSDate *date [NSDate dateWithTimeIntervalSince1970:0]; NSDate *date2 [NSDate dateWithTimeInterval:50 sinceDate:date]; NSInteger seconds [date timeIntervalSinceDate:date2]; NSLog(%i,seconds); 将返回结果-50date为1970-1-1 0:0:0date2为1970-1-1 0:0:50从date2的时间开始倒退50秒到达date的时间。 NSTimeZone NSTimeZone表示时区信息。 有下面几种初始化方法 1.  (id)timeZoneWithName:(NSString *)aTimeZoneName / - (id)initWithName:(NSString *)aName 根据时区名称初始化。可以调用NSTimeZone的类方法  (NSArray *)knownTimeZoneNames来返回所有已知的时区名称。 NSTimeZone *zone [[NSTimeZone alloc] initWithName:America/Chicago]; //NSTimeZone *zone [NSTimeZone timeZoneWithName:America/Chicago]; NSLog(%,zone); 打印出America/Chicago (CST) offset -21600 2.  (id)timeZoneWithAbbreviation:(NSString *)abbreviation 根据时区缩写初始化。例如EST(美国东部标准时间)、HKT(香港标准时间) NSTimeZone *zone [NSTimeZone timeZoneWithAbbreviation:EST]; NSLog(%,zone); 打印出Asia/Hong_Kong (HKT) offset 28800 3.  (NSTimeZone *)systemTimeZone 返回系统时区 NSTimeZone *zone [NSTimeZone systemTimeZone]; NSLog(%,zone); 假如时区是上海打印出的时区信息将会是Asia/Shanghai (CST (China)) offset 2880028800代表相对于GMT时间偏移的秒数即8个小时。(8*60*60) 4.  (NSTimeZone *)localTimeZone 返回本地时区与systemTimeZone的区别在于本地时区可以被修改而系统时区不能修改。 [NSTimeZone setDefaultTimeZone:[[NSTimeZone alloc] initWithName:America/Chicago]];NSTimeZone *systemZone [NSTimeZone systemTimeZone]; NSTimeZone *localZone [NSTimeZone localTimeZone];NSLog(%,systemZone); NSLog(%,localZone); 打印出的系统时区仍然是Asia/Shanghai (CST (China)) offset 28800而本地时区经过修改后变成了Local Time Zone (America/Chicago (CST) offset -21600) 5.  (id)timeZoneForSecondsFromGMT:(NSInteger)seconds 根据零时区的秒数偏移返回一个新时区对象 NSTimeZone *zone [NSTimeZone timeZoneForSecondsFromGMT:28800]; NSLog(%,zone); 打印出GMT0800 (GMT08:00) offset 28800 NSTimeZone常用对象方法与类方法 1.  (NSArray *)knownTimeZoneNames 以数组的形式返回所有已知的时区名称 NSArray *zoneArray [NSTimeZone knownTimeZoneNames]; for(NSString *str in zoneArray) {NSLog(%,str); } 2. - (NSString *)name / - (NSString *)abbreviation 返回时区对象的名称或缩写 NSTimeZone *zone [NSTimeZone localTimeZone]; NSString *strZoneName [zone name]; NSString *strZoneAbbreviation [zone abbreviation]; NSLog(name is %,strZoneName); NSLog(abbreviation is %,strZoneAbbreviation); name is Asia/Hong_Kong abbreviation is HKT 3. - (NSInteger)secondsFromGMT 得到当前时区与零时区的间隔秒数 NSTimeZone *zone [NSTimeZone localTimeZone]; int seconds [zone secondsFromGMT]; NSLog(%i,seconds); NSLoale NSLoale类返回本地化信息主要体现在语言和区域格式这两个设置项。有下面几种初始化方法 1.  (id)systemLocale 返回系统初始本地化信息 NSLocale *locale [NSLocale systemLocale]; NSLog(%,[[locale objectForKey:NSLocaleCalendar] calendarIdentifier]); 2.  (id)currentLocale / (id)autoupdatingCurrentLocale 这两个类方法都将返回当前客户端的本地化信息区别在于currentLocale取得的值会一直保持在cache中第一次用此方法实例化对象后即使修改了本地化设定这个对象也不会改变。而使用autoupdatingCurrentLocale当每次修改本地化设定其实例化的对象也会随之改变。 下面的代码演示了区别所在假设初始本地化信息为en_US先用这两个函数分别初始化两个对象然后修改本地化设定语言为台湾繁体中文再重新打印这两个对象的信息 NSLocale *locale1; NSLocale *locale2;- (IBAction)doTest:(id)sender {locale1 [NSLocale currentLocale];locale2 [NSLocale autoupdatingCurrentLocale];NSLog(%,locale1.localeIdentifier); //print en_USNSLog(%,locale2.localeIdentifier); //print en_US }- (IBAction)doAgain:(id)sender {NSLog(%,locale1.localeIdentifier); //print en_USNSLog(%,locale2.localeIdentifier); //print zh_TW } 3. - (id)initWithLocaleIdentifier:(NSString *)string 用标示符初始化本地化信息 NSLocale *locale [[NSLocale alloc] initWithLocaleIdentifier:zh_CN]; NSString *strSymbol [locale objectForKey:NSLocaleCurrencySymbol]; NSLog(%,strSymbol); 代码用zh_CN来初始化对象然后再打印出对象的货币符号得到的结果是人民币符号 NSLoale常用对象方法与类方法 1. - (id)objectForKey:(id)key 根据不同的key返回各种本地化信息例如下面的代码返回了当前货币符号 NSLocale *locale [NSLocale currentLocale]; NSString *strSymbol [locale objectForKey:NSLocaleCurrencySymbol]; NSCalendar *calendar [[NSLocale currentLocale] objectForKey:NSLocaleCalendar]; 2. - (NSString *)displayNameForKey:(id)key value:(id)value 显示特定地区代号下相应键的显示名称 NSLocale *locale [[NSLocale alloc] initWithLocaleIdentifier:zh_CN]; NSString *str [locale displayNameForKey:NSLocaleIdentifier value:en_US]; NSLog(%,str); 第一句代码代表以中文来实例化对象然后得到en_US的NSLocaleIdentifier键的显示名称。最后输出的结果是英文美国 NSDateComponents NSDateComponents封装了具体年月日、时秒分、周、季度等 NSDateComponents *compt [[NSDateComponents alloc] init]; [compt setEra:1]; [compt setYear:2013]; [compt setMonth:3]; [compt setDay:15]; [compt setHour:11]; [compt setMinute:20]; [compt setSecond:55]; [compt setQuarter:2]; [compt setTimeZone:[NSTimeZone systemTimeZone]]; [compt setWeek:3]; [compt setWeekday:4]; [compt setWeekOfMonth:3]; [compt setWeekOfYear:2]; [compt setCalendar:[NSCalendar currentCalendar]]; NSDateComponents相关方法 1. NSCalendar对象的 - (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)date 取得一个NSDate对象的1个或多个部分用NSDateComponents来封装 NSCalendar *calendar [NSCalendar currentCalendar]; NSDate *date [NSDate date]; //NSDateComponents *compt [calendar components:NSDayCalendarUnit fromDate:date]; NSDateComponents *compt [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:date];NSLog(%d,%,[compt year],date); NSLog(%d,%,[compt month],date); NSLog(%d,%,[compt day],date); 需要注意的是只有明确指定了unitFlagsNSDateComponents相应的那一部分才有值。 2. NSCalendar对象的 - (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate*)resultDate options : (NSUInteger)opts 取得两个NSDate对象的时间间隔用NSDateComponents来封装 NSCalendar *calendar [NSCalendar currentCalendar]; NSDate *date [NSDate date]; NSDate *date2 [NSDate dateWithTimeInterval:5*60*6075 sinceDate:date]; NSDateComponents *compt [calendar components:(NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date toDate:date2 options:0];NSLog(%d,[compt minute]); NSLog(%d,[compt second]); 有几点需要注意 ① 得到的NSDateComponents对象可能会包含负数。例如当toDate比fromDate晚10秒second部分返回10当toDate比fromDate早10秒second部分返回-10 ② 当指定unitFlags返回多个部分时相隔的时间由多个部分共同组成(而不是独立去表示)。例如上面的例子时间相差5小时1分15秒如果指定只返回second部分将得到18075秒如果指定返回minute和second部分将得到301分15秒如果指定返回hour、minute和second将得到5小时1分15秒。 3. NSCalendar对象的 - (NSDate *)dateFromComponents:(NSDateComponents *)comps 根据NSDateComponents对象得到一个NSDate对象 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(%,localeDate); 4. NSCalendar对象的 - (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSUInteger)opts 在参数date基础上增加一个NSDateComponents类型的时间增量 NSDateComponents *compt [[NSDateComponents alloc] init]; [compt setDay:25]; [compt setHour:4]; [compt setMinute:66];NSCalendar *calendar [NSCalendar currentCalendar]; NSDate *date [calendar dateByAddingComponents:compt toDate:[NSDate date] options:0];//得到本地时间避免时区问题 NSTimeZone *zone [NSTimeZone systemTimeZone]; NSInteger interval [zone secondsFromGMTForDate:date]; NSDate *localeDate [date dateByAddingTimeInterval:interval];NSLog(%,localeDate); 当前时间的基础上增加25天4小时66秒 NSCalendar 1.  (id)currentCalendar / (id)autoupdatingCurrentCalendar 这两个类方法都将返回当前客户端的逻辑日历区别在于currentCalendar取得的值会一直保持在cache中第一次用此方法实例化对象后即使修改了系统日历设定这个对象也不会改变。而使用autoupdatingCurrentCalendar当每次修改系统日历设定其实例化的对象也会随之改变。 下面的代码演示了区别所在假设初始Calendar设定为NSGregorianCalendar(公历)先用这两个函数分别初始化两个对象然后修改系统日历为NSJapaneseCalendar(日本和历)再重新打印这两个对象的信息 NSCalendar *calendar; NSCalendar *calendar2;- (IBAction)doTest:(id)sender {calendar [NSCalendar currentCalendar];calendar2 [NSCalendar autoupdatingCurrentCalendar];NSLog(%,calendar.calendarIdentifier); //print gregorianNSLog(%,calendar2.calendarIdentifier); //print gregorian }- (IBAction)doAgain:(id)sender {NSLog(%,calendar.calendarIdentifier); //print gregorianNSLog(%,calendar2.calendarIdentifier); //print japanese } 2. - (id)initWithCalendarIdentifier:(NSString *)string 根据提供的日历标示符初始化 NSCalendar *calendar [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar]; NSLog(%,calendar.calendarIdentifier); 系统中定义的日历有 NSGregorianCalendar -- 公历NSBuddhistCalendar -- 佛教日历NSChineseCalendar -- 中国农历NSHebrewCalendar -- 希伯来日历NSIslamicCalendar -- 伊斯兰历NSIslamicCivilCalendar -- 伊斯兰教日历NSJapaneseCalendar -- 日本日历NSRepublicOfChinaCalendar -- 中华民国日历台湾NSPersianCalendar -- 波斯历NSIndianCalendar -- 印度日历NSISO8601Calendar -- ISO8601 NSCalendar常用对象方法与类方法 1. - (void)setLocale:(NSLocale *)locale 设置本地化信息 NSCalendar *calendar [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:zh_CN]]; NSLog(%,calendar.locale.localeIdentifier); 2. - (void)setTimeZone:(NSTimeZone *)tz 设置时区信息 NSCalendar *calendar [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:HKT]]; NSLog(%,calendar.timeZone); 3. - (void)setFirstWeekday:(NSUInteger)weekday 设置每周的第一天从星期几开始比如1代表星期日开始2代表星期一开始以此类推。默认值是1 如图所示如果从星期天开始日历的表现形式   如果从星期二开始日历的表现形式 NSCalendar *calendar [NSCalendar currentCalendar]; [calendar setFirstWeekday:3]; NSLog(%i,calendar.firstWeekday); 4. - (void)setMinimumDaysInFirstWeek:(NSUInteger)mdw 设置每年及每月第一周必须包含的最少天数比如设定第一周最少包括3天则value传入3 NSCalendar *calendar [NSCalendar currentCalendar]; [calendar setMinimumDaysInFirstWeek:3]; NSLog(%i,calendar.minimumDaysInFirstWeek); 5. - (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date 获取一个小的单位在一个大的单位里面的序数 NSCalendarUnit包含的值有 NSEraCalendarUnit -- 纪元单位。对于NSGregorianCalendar(公历)来说只有公元前(BC)和公元(AD)而对于其它历法可能有很多例如日本和历是以每一代君王统治来做计算。NSYearCalendarUnit -- 年单位。值很大相当于经历了多少年未来多少年。NSMonthCalendarUnit -- 月单位。范围为1-12NSDayCalendarUnit -- 天单位。范围为1-31NSHourCalendarUnit -- 小时单位。范围为0-24NSMinuteCalendarUnit -- 分钟单位。范围为0-60NSSecondCalendarUnit -- 秒单位。范围为0-60NSWeekCalendarUnit -- 周单位。范围为1-53NSWeekdayCalendarUnit -- 星期单位每周的7天。范围为1-7NSWeekdayOrdinalCalendarUnit -- 没完全搞清楚NSQuarterCalendarUnit -- 几刻钟也就是15分钟。范围为1-4NSWeekOfMonthCalendarUnit -- 月包含的周数。最多为6个周NSWeekOfYearCalendarUnit -- 年包含的周数。最多为53个周NSYearForWeekOfYearCalendarUnit -- 没完全搞清楚NSTimeZoneCalendarUnit -- 没完全搞清楚 下面是一些示例 ① 当小单位为NSWeekdayCalendarUnit大单位为NSWeekCalendarUnit时(即某个日期在这一周是第几天)根据firstWeekday属性不同返回的结果也不同。 NSCalendar *calendar [NSCalendar currentCalendar]; NSDate *date [NSDate dateWithTimeIntervalSinceReferenceDate:10]; //[calendar setFirstWeekday:2]; int count [calendar ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:date]; NSLog(%d,count); 默认firstWeekday为1(星期天开始)的情况下得到的结果是2从下图可以看到是第2天。 假如firstWeekday被设置为2(星期一开始)的情况下得到的结果是1从下图可以看到是第1天 ② 当小单位为NSWeekCalendarUnit大单位为NSYearCalendarUnit时(即某个日期在这一年中是第几周)根据minimumDaysInFirstWeek属性不同返回的结果也不同。 NSDateComponents *compt [[NSDateComponents alloc] init]; [compt setYear:2013]; [compt setMonth:1]; [compt setDay:20];NSCalendar *calendar [NSCalendar currentCalendar];NSDate *date [calendar dateFromComponents:compt];//[calendar setMinimumDaysInFirstWeek:6]; int count [calendar ordinalityOfUnit:NSWeekCalendarUnit inUnit:NSYearCalendarUnit forDate:date]; NSLog(%d,count); 从上图的日历中可以看出在没有设置minimumDaysInFirstWeek的情况下1月20日得到的结果是4(第四个周)。 默认情况下第一个周有5天如果将minimumDaysInFirstWeek设置为6天则原本是第一周的1月1日--1月5日被划分到了上一年返回0而1月6日--1月12日升为第一周1月13日--1月19日升为第二周。。依此类推。 所以需要关注的是minimumDaysInFirstWeek与实际第一周包含天数的大小比较如果提供的minimumDaysInFirstWeek比实际第一周的天数小则一切不变否则统计一年中第几周、一个月中第几周会产生变化。 6. - (NSRange)rangeOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date 根据参数提供的时间点得到一个小的单位在一个大的单位里面的取值范围 NSDateComponents *compt [[NSDateComponents alloc] init]; [compt setYear:2013]; [compt setMonth:2]; [compt setDay:21]; [compt setHour:9]; [compt setMinute:45]; [compt setSecond:30];NSCalendar *calendar [NSCalendar currentCalendar]; NSDate *date [calendar dateFromComponents:compt];//得到本地时间避免时区问题 NSTimeZone *zone [NSTimeZone systemTimeZone]; NSInteger interval [zone secondsFromGMTForDate:date]; NSDate *localeDate [date dateByAddingTimeInterval:interval];NSRange range [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:localeDate];NSLog(%d -- %d,range.location,range.length); 调用这个方法要明确一点取得的是范围而不是包含下面是一些例子 ① 小单位是NSDayCalendarUnit大单位是NSYearCalendarUnit并不是要取这一年包含多少天而是要取天(Day)这个单位在这一年(Year)的取值范围。其实不管你提供的日期是多少返回的值都是1--31。 ② 小单位是NSDayCalendarUnit大单位是NSMonthCalendarUnit。要取得参数时间点所对应的月份下天(Day)的取值范围。根据参数时间的月份不同值也不同。例如2月是1--28、3月是1--31、4月是1--30。 ③ 小单位是NSWeekCalendarUnit大单位是NSMonthCalendarUnit。要取得参数时间点所对应的月份下周(Week)的取值范围。需要注意的是结果会受到minimumDaysInFirstWeek属性的影响。在默认minimumDaysInFirstWeek情况下取得的范围值一般是1--5从日历上可以看出来这个月包含5排即5个周。 ④ 小单位是NSDayCalendarUnit大单位是NSWeekCalendarUnit。要取得周所包含的天(Day)的取值范围。下面是一个示例日历图 在上图的日期条件下假如提供的参数是4月1日--4月6日那么对应的week就是1(第一个周)可以看到第一个周包含有6天从1号开始那么最终得到的范围值为1--6。 假如提供的参数是4月18日那么对应的week是3(第三个周)第三个周包含有7天从14号开始那么最终得到的范围值是14--7。 假如提供的参数是4月30日那么对应的week是5(第五个周)第五个周只包含3天从28号开始那么最终得到的范围值是28--3。 7. - (BOOL)rangeOfUnit:(NSCalendarUnit)unit startDate:(NSDate **)datep interval:(NSTimeInterval *)tip forDate:(NSDate *)date 根据参数提供的时间点返回所在日历单位的开始时间。如果startDate和interval均可以计算则返回YES否则返回NO unit -- 日历单位datep -- 开始时间通过参数返回tip -- 日历单位所对应的秒数通过参数返回date -- 时间点参数 NSDate *dateOut nil; NSTimeInterval count 0;NSDateComponents *compt [[NSDateComponents alloc] init]; [compt setYear:2013]; [compt setMonth:3]; [compt setDay:22];NSCalendar *calendar [NSCalendar currentCalendar]; NSDate *date [calendar dateFromComponents:compt]; BOOL b [calendar rangeOfUnit:NSMonthCalendarUnit startDate:dateOut interval:count forDate:date]; if(b) {//得到本地时间避免时区问题NSTimeZone *zone [NSTimeZone systemTimeZone];NSInteger interval [zone secondsFromGMTForDate:dateOut];NSDate *localeDate [dateOut dateByAddingTimeInterval:interval];NSLog(%,localeDate);NSLog(%f,count); } else {NSLog(无法计算); } 上面的例子要求返回2013年3月22日当月的起始时间以及当月的秒数。得到的结果是2013-03-01 00:00:00 00002678400。(2678400 31天 * 24小时 * 60分 * 60秒)。 假如将上面的日历单位改为NSWeekCalendarUnit那么得到的结果是2013-03-17 00:00:00 0000604800。当周的第一天是3月17日。(604800 7天 * 24小时 * 60分 * 60秒)。 假如将上面的日历单位改为NSYearCalendarUnit那么得到的结果是2013-01-01 00:00:00 000031536000。这一年的第一天是1月1日(31536000  365天 * 24小时 * 60分 * 60秒)。 NSDateFormatter NSDateFormatter的日期格式如下: G -- 纪元 一般会显示公元前(BC)和公元(AD)y -- 年 假如是2013年那么yyyy2013yy13  M -- 月 假如是3月那么M3MM03MMMMarMMMMMarch 假如是11月那么M11MM11MMMNovMMMMNovemberw -- 年包含的周 假如是1月8日那么w2(这一年的第二个周)W -- 月份包含的周(与日历排列有关) 假如是2013年4月21日那么W4(这个月的第四个周)F -- 月份包含的周(与日历排列无关) 和上面的W不一样F只是单纯以7天为一个单位来统计周例如7号一定是第一个周15号一定是第三个周与日历排列无关。D -- 年包含的天数 假如是1月20日那么D20(这一年的第20天) 假如是2月25日那么D312556(这一年的第56天)d -- 月份包含的天数 假如是5号那么d5dd05 假如是15号那么d15dd15E -- 星期  假如是星期五那么EFriEEEEFridaya -- 上午(AM)/下午(PM)H -- 24小时制显示为0--23 假如是午夜00:40那么H0:40HH00:40h -- 12小时制显示为1--12 假如是午夜00:40那么h12:40K -- 12小时制显示为0--11 假如是午夜00:40那么K0:40KK00:40k -- 24小时制显示为1--24 假如是午夜00:40那么k24:40m -- 分钟 假如是5分钟那么m5mm05 假如是45分钟那么m45mm45s -- 秒 假如是5秒钟那么s5ss05 假如是45秒钟那么s45ss45S -- 毫秒 一般用SSS来显示z -- 时区 表现形式为GMT08:00Z -- 时区 表现形式为0800 NSDateFormatter的两个最实用的方法是dateFromString和stringFromDate前者将一个字符串经过格式化后变成NSDate对象后者将NSDate对象格式化成字符串。 在调用setDateFormat设置格式化字符串时可以加入一些别的字符串用单引号来引入例如 [formatter setDateFormat:yyyy-MM-dd some special string HH:mm:ss]; 使用NSDateFormatter转换时间字符串时默认的时区是系统时区例如在中国一般都是北京时间(8)如果直接转换会导致结果相差8小时所以一般的做法是先指定时区为GMT标准时间再转换例如 NSDateFormatter *formatter [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; [formatter setDateFormat:yyyy-MM-dd HH:mm:ss z];NSDateComponents *compt [[NSDateComponents alloc] init]; [compt setYear:2013]; [compt setMonth:3]; [compt setDay:13]; [compt setHour:1]; [compt setMinute:55]; [compt setSecond:28];NSCalendar *calendar [NSCalendar currentCalendar]; [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSDate *date [calendar dateFromComponents:compt]; NSLog(%,date); NSString *str [formatter stringFromDate:date]; NSLog(%,str); 转载于:https://www.cnblogs.com/litfu-frenziedworld/p/3359213.html
http://wiki.neutronadmin.com/news/49146/

相关文章:

  • 免费邮箱登录入口aso优化怎么做
  • 成都企业网站开发企业百度网站建设
  • 网站建设综合推荐自媒体
  • 德州中文网站建设高端网站开发环境
  • 如何建立网站会员系统吗泰州市建设监理协会网站
  • 勤哲网站开发视频大连企业免费建站
  • wordpress p=29cpu优化软件
  • 备案网站建设商标查询网站建设
  • 广州营销型网站建设价格网站插件代码大全
  • 用户登录网站开发图片制作视频
  • 有没有免费建网站网站建设大概价格
  • 专门卖化妆品网站建设建设网站的费用吗
  • 网站备案之前需要建好网站吗原创wordpress主题
  • 做网上竞猜网站合法吗金网科技
  • 残联网站建设工业和信息化部五系网站建设
  • 网站建设类大连网站制作咨询
  • 设计好的集团网站建设多少钱太原 招聘 网站建设 技术经理
  • 北海哪家做网站批量修改wordpress文章分类目录
  • 国内哪个网站是做电子元器件的专门 做鞋子团购的网站
  • 个人网站域名取名天河做网站平台
  • 建立网站流程图wordpress 音乐
  • 网站开发所需要的的环境网站开发竞争性谈判
  • 素材网站个人做的做建网站的工作一年赚几百万
  • 旅游订票网站开发公司商标设计
  • 蓝色科技企业网站模板android开发基础教程
  • 网站建站建设多少钱软件实施工资一般多少
  • 网站轮播图能用什么软件做dw做网站怎么排版
  • 2018年怎么做网站排名网站推广的优点
  • 网站备案信息查询申请表学校网站模板大全
  • 石狮网站设计公司中国工信部网站备案