博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS 时间 日期 用法大全
阅读量:5301 次
发布时间:2019-06-14

本文共 8301 字,大约阅读时间需要 27 分钟。

1.NSTimeInterval: 时间戳,以double为单位2.系统返回的时间都是格林尼治时间,要得到北京时间需要转化NSDate类:@interface NSDate (NSDateCreation)中方法:创建或初始化NSDate值NSDate返回日期@interface NSDate (NSExtendedDate): NSDate的扩张方法方法一:日前之间的前后比较方法二:得到距离某一日起的时间间隔(时间戳)方法三:得到在当前实例中保存的时间为基准, 过了sec的日期方法四:将时间表示字符串:description  其实这个方法写不写一样 系统会自动的参考:冷雨之家博客对该类方法作了详尽的描述:http://hi.baidu.com/douxinchun/item/86c9732bc603349db73263aeNSDateComponents类:()NSDateComponents封装在一个可扩展的,面向对象的方式的日期组件。它是用来弥补时间的日期和时间组件提供一个指定日期:小时,分钟,秒,日,月,年,等等。它也可以用来指定的时间,例如,5小时16分钟。一个NSDateComponents对象不需要定义所有组件领域。当一个NSDateComponents的新实例被创建,日期组件被设置为NSUndefinedDateComponent。一个NSDateComponents对象本身是毫无意义的;你需要知道它是针对什么日历解释,你需要知道它的值是否是正整数和值是多少。NSDateComponents的实例不负责回答关于一个日期以外的信息,它是需要先初始化的。例如,如果你初始化一个对象为2004年5月6日,其星期几NSUndefinedDateComponent,不是星期四。要得到正确的星期几,你必须创建一个NSCalendar日历实例,创建一个NSDate对象并使用dateFromComponents:方法,然后使用components:fromDate:检索平周几NSCalendar类:(日历)用于处理时间相关问题。比如比较时间前后、计算日期所的周别等。//在当前日期上加上设定的值得到的日期NSDate *newdate = [calendar dateByAddingComponents:comps toDate:currentDate  options:0];参考:NSCalendar类方法的详细介绍:http://www.cnblogs.com/wujian1360/archive/2011/09/05/2168007.htmlNSDateFormatter类:  设定时间的格式用于格式化NSDate对象,支持本地化的信息。setDateStyle和setTimeStyle可同时设置,但不能和setDateFormat共存,只可选其一//设置为中国格式输出dateFormater.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置日期、时间显示格式;- (void)setDateStyle:(NSDateFormatterStyle)style;- (void)setTimeStyle:(NSDateFormatterStyle)style;NSDateFormatterStyle系统选项包含的Style:{    NSDateFormatterNoStyle,    NSDateFormatterShortStyle,     // “11/23/37” or “3:30pm”.    NSDateFormatterMediumStyle,     //“Nov 23, 1937”.or “3:30:32pm”.       NSDateFormatterLongStyle,     //“November 23, 1937” or “3:30:32pm”.  GMT+08:00    NSDateFormatterFullStyle     //“Tuesday, April 12, 1952 AD” or “3:30:42pm PST”.}而经常使用的会是setDateFormat方法,可自定义输出格式- (void)setDateFormat:(NSString *)string;SymbolMeaningExampleGera designatorGADyyearyyyyyy or y961996Yyear of "Week of Year"Y1997uextended yearu4601Ucyclic year name, as in Chinese lunar calendarU甲子QquarterQ or QQQQQQQQQ02Q22nd quarterqStand Alone quarterq or qqqqqqqqq02Q22nd quarterMmonth in yearM or MMMMMMMMMMMMMM09SeptSeptemberSLStand Alone month in yearL or LLLLLLLLLLLLLL09SeptSeptemberSwweek of yearw or ww27Wweek of monthW2dday in monthddd202Dday of yearD189Fday of week in month2 (2nd Wed in July)gmodified julian dayg2451334Eday of weekE, EE, or EEEEEEEEEEEETuesTuesdayTelocal day of weekexample: if Monday is 1st day, Tuesday is 2nd )e or eeeeeeeeeeeeee2TuesTuesdayTcStand Alone local day of weeke or eeeeeeeeeeeeee2TuesTuesdayTaam/pm markerapmhhour in am/pm (1~12)hhh707Hhour in day (0~23)HHH000khour in day (1~24)kkk2424Khour in am/pm (0~11)KKK000mminute in hourmmm404ssecond in minutesss505Smillisecond (maximum of 3 significant digits);for S or SS, truncates to the number of lettersfor SSSS or longer, fills additional places with 0SSSSSSSSSS2232352350Amilliseconds in dayA61201235zTime Zone: specific non-locationz, zz, or zzzzzzzPDTPacific Daylight TimeZTime Zone: RFC 822Time Zone: localized GMTTIme Zone: ISO8601Z, ZZ, or ZZZZZZZZZZZZ-0800GMT-08:00-08:00vTime Zone: generic non-locationvvvvvPTPacific Time or United States (Los Angeles)VTime Zone: specific non-location, identical to zVPDTVVVVTime Zone: generic locationVVVVUnited States (Los Angeles)Wweek in month2'escape for text'(nothing)' 'two single quotes produce one' '' Format PatternResultyyyy.MM.dd G 'at' HH:mm:ss zzz1996.07.10 AD at 15:08:56 PDTEEE, MMM d, ''yyWed, July 10, '96h:mm a12:08 PMhh 'o''clock' a, zzzz12 o'clock PM, Pacific Daylight Time K:mm a, z0:00 PM, PSTyyyyy.MMMM.dd GGG hh:mm aaa 01996.July.10 AD 12:08 PMNSDateFormatter的格式串详解:a: AM/PM (上午/下午)A: 0~86399999 (一天的第A微秒)c/cc: 1~7 (一周的第一天, 周天为1)ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写)cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期几全拼)d: 1~31 (月份的第几天, 带0)D: 1~366 (年份的第几天,带0)e: 1~7 (一周的第几天, 带0)E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写)EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期几全拼)F: 1~5 (每月的第几周, 一周的第一天为周一)g: Julian Day Number (number of days since 4713 BC January 1) 未知G~GGG: BC/AD (Era Designator Abbreviated) 未知GGGG: Before Christ/Anno Domini 未知h: 1~12 (0 padded Hour (12hr)) 带0的时, 12小时制H: 0~23 (0 padded Hour (24hr))  带0的时, 24小时制k: 1~24 (0 padded Hour (24hr) 带0的时, 24小时制K: 0~11 (0 padded Hour (12hr)) 带0的时, 12小时制         L/LL: 1~12 (0 padded Month)  第几月LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec 月份简写LLLL: January/February/March/April/May/June/July/August/September/October/November/December 月份全称         m: 0~59 (0 padded Minute) 分钟M/MM: 1~12 (0 padded Month) 第几月MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/DecMMMM: January/February/March/April/May/June/July/August/September/October/November/December         q/qq: 1~4 (0 padded Quarter) 第几季度qqq: Q1/Q2/Q3/Q4 季度简写qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter 季度全拼Q/QQ: 1~4 (0 padded Quarter) 同小写QQQ: Q1/Q2/Q3/Q4 同小写QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter 同小写s: 0~59 (0 padded Second) 秒数S: (rounded Sub-Second) 未知         u: (0 padded Year) 未知         v~vvv: (General GMT Timezone Abbreviation) 常规GMT时区的编写vvvv: (General GMT Timezone Name) 常规GMT时区的名称         w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year) 一年的第几周, 一周的开始为周日,第一周从去年的最后一个周日起算W: 1~5 (0 padded Week of Month, 1st day of week = Sunday) 一个月的第几周         y/yyyy: (Full Year) 完整的年份yy/yyy: (2 Digits Year)  2个数字的年份Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year) 这个年份未知干嘛用的YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year) 这个年份未知干嘛用的         z~zzz: (Specific GMT Timezone Abbreviation) 指定GMT时区的编写zzzz: (Specific GMT Timezone Name) Z: +0000 (RFC 822 Timezone) 指定GMT时区的名称eg:NSString lastModifiedString = @"Mon, 06 Jun 2011 12:47:05 GMT"; NSDateFormatter *df = [[NSDateFormatter alloc] init];   //df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; df.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss z"; df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];   df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];  NSDate date = [df dateFromString:lastModifiedString];  设置上下午显示格式:NSDateFormatter* formatter = [[[NSDateFormatteralloc] init] autorelease];[formatter setDateFormat:dateFormatterString];[formatter setAMSymbol:@"am"];    //[formatter setPMSymbol:@"pm"];return [formatter stringFromDate:self];参考:NSDateFormatter格式一览表:http://www.th7.cn/Program/IOS/201210/109696.shtml开源项目库文档:http://userguide.icu-project.org/formatparse/datetime (我是没怎么看懂) NSDateFormatter的格式串详解:http://linuxp.blog.163.com/blog/static/17096277201221313933377/ NSTimeZone:时区    NSDate *date = [NSDate date];    NSLog(@"date:%@",date);    //系统时区    NSTimeZone *zone = [NSTimeZone systemTimeZone];    NSData *data = [zone data];    NSLog(@"data:%@",data);    //时间缩写    NSString *abbStr = [zone abbreviation];    NSLog(@"abbStr:%@",abbStr);    //当地时区    NSTimeZone *localTime = [NSTimeZonelocalTimeZone];    NSLog(@"localTime:%@",localTime);    //和格林尼治时间差    NSInteger timeOff = [zone secondsFromGMT];    //视察转化    NSDate *timeOffDate = [date dateByAddingTimeInterval:timeOff];    NSLog(@"timeOffDate:%@",timeOffDate);    //所有地区名称    NSArray *zoneArrs = [NSTimeZone knownTimeZoneNames];    NSLog(@"zoneArrs:%@",zoneArrs);    //遍历    for (NSString *names in zoneArrs) {        //时区        NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:names];        //设置格式        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];        [dateFormatter setDateStyle:kCFDateFormatterFullStyle];        [dateFormatter setTimeStyle:kCFDateFormatterFullStyle];        [dateFormatter setTimeZone:timeZone];        NSString *string = [dateFormatter stringFromDate:date];        NSLog(@"string:%@,[timeZone name]:%@",string,[timeZone name]);        [dateFormatter release];        [timeZone release];    }获取中国标准时间(没有看见有北京时区的,选了上海的)    NSDate *date = [NSDatedate];    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]autorelease];    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss EEEE";    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];    [dateFormatter setTimeZone:timeZone];    NSLog(@"时间:%@",[dateFormatter stringFromDate:date]);

转载于:https://www.cnblogs.com/yingkong1987/archive/2013/06/05/3119488.html

你可能感兴趣的文章
2018-2019-2 20165314『网络对抗技术』Exp5:MSF基础应用
查看>>
Python-S9-Day127-Scrapy爬虫框架2
查看>>
SecureCRT的使用方法和技巧(详细使用教程)
查看>>
右侧导航栏(动态添加数据到list)
查看>>
81、iOS本地推送与远程推送详解
查看>>
虚拟DOM
查看>>
自建数据源(RSO2)、及数据源增强
查看>>
BootStrap2学习日记2--将固定布局换成响应式布局
查看>>
关于View控件中的Context选择
查看>>
2018icpc徐州OnlineA Hard to prepare
查看>>
Spark的启动进程详解
查看>>
使用命令创建数据库和表
查看>>
数据库的高级查询
查看>>
机器视觉:SSD Single Shot MultiBox Detector
查看>>
201521123044 《Java程序设计》第1周学习总结
查看>>
MIT Scheme 的基本使用
查看>>
程序员的“机械同感”
查看>>
在16aspx.com上下了一个简单商品房销售系统源码,怎么修改它的默认登录名和密码...
查看>>
c++回调函数
查看>>
linux下Rtree的安装
查看>>