对于NSDate常用操作整理,如有不足请补充
1,获取当前时区时间
//初始化时间段
NSDate *date = [NSDate date];
//获取当前时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
//以秒为单位返回当前应用程序与世界标准时间(格林威尼时间)的时差
NSInteger interval = [zone secondsFromGMTForDate: date];
//补充时差后为当前时间
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
2,将字符串格式化NSDate
NSString *dateStr=@"2013-08-13 20:28:40";
//格式化
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *fromdate=[dateFormatter dateFromString:dateStr];
//时区
NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
//同例1
NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
NSDate *fromDate = [fromdate dateByAddingTimeInterval: frominterval];
NSLog(@"fromdate=%@",fromDate);
//将NSDate转为NSString
NSString *strTime = [fromDate description];
//如果直接将NSDate格式化
NSDate *recv = [NSDate dateFromString:recvTime withFormat:@"yyyy-MM-dd HH:mm:ss Z"];
3,比较两个时间相差(年月日,时分秒)
double intervalTime = [fromDate timeIntervalSinceReferenceDate] - [localeDate timeIntervalSinceReferenceDate];
long lTime = (long)intervalTime;
NSInteger iSeconds = lTime % 60;
NSInteger iMinutes = (lTime / 60) % 60;
NSInteger iHours = (lTime / 3600)$;
NSInteger iDays = lTime/60/60/24;
NSInteger iMonth = lTime/60/60/24/12;
NSInteger iYears = lTime/60/60/24/384;
NSLog(@"相差M年d月 或者 d日d时d分d秒", iYears,iMonth,iDays,iHours,iMinutes,iSeconds);
4,按日期排序
//与otherDate比较,相同返回YES
- (BOOL)isEqualToDate:(NSDate *)otherDate;
//与anotherDate比较,返回较早的那个日期
- (NSDate *)earlierDate:(NSDate *)anotherDate;
//与anotherDate比较,返回较晚的那个日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
//该方法用于排序时调用
- (NSComparisonResult)compare:(NSDate *)other;
//. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
//. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
//. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending
5,从NSDate中获取年月日,时分秒
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
int year = [comps year];
int month = [comps month];
int day = [comps day];
int hour = [comps hour];
int min = [comps minute];
int sec = [comps second];
//获取数据有异常
// NSError *error = [[NSError alloc] init];
// NSData *pData = = [NSData dataWithContentsOfURL:URL options:NSDataReadingMapped error:&error];
// NSLog(@"download string end error = %d", [error code]);
// [error release];
判断12小时或24小时(制)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"a"];
NSString *AMPMtext = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
if ([AMPMtext isEqualToString:@""]){
NSLog(@"24小时制");
}
else {
NSLog(@"12小时制");
}
分享到:
相关推荐
Swift-NSDate-Extensions 是 Swift NSDate 扩展,帮助开发。 标签:Swift
NSDate-DaboExtensionNSDate分类支持Pod导入pod 'NSDate-DaboExtension', '~> 1.0.0'
NSDate-TimeOperators 用于 NSDate 上基本比较和时间操作的 Swift 扩展 比较 这些扩展向 NSDate 添加了基本的比较运算符 <、>、<=、>=、==、!=。 所以而不是像这样的东西: firstDate.compare(secondDate)...
NSDate-RFC1123 在Swift中实现HTTP日期 ,以解析类似Last-Modified的HTTP标头
DateHelper - NSDate在Swift中的便利扩展
从那以后,NSDate扩展 在NSDate中添加一个名为since()的简单方法,以便您可以获取当时与现在之间的时差(即“ 2小时前... 使用时,只需在项目中包含NSDate-Extensions.swift并在任何NSDate对象上调用.since()即可。
NSDate+RORandom Keith Smiley 的类别扩展为包括在两个给定日期之间创建随机 NSDates 的方法。 对于日期顺序很重要的测试软件。 pod 'NSDate-RORandom'
NSDate助手 NSDate上的类别,包括使用日期和时间的有用方法。 TimeZone和DateFormat有ENUM : ... 以及常用时间间隔(以秒为单位)的常量: static NSUInteger const BSMinute = 60 ; static NSUInteger const BSH
NSDate总结,NSDateFormatter总结,NSCalender总结,倒计时(60秒倒计时,天数倒计时,cell上天数倒计时)
NULL 博文链接:https://o0o0o0o.iteye.com/blog/588887
作者alexruperez,源码NSDate-Extension,NSDate-Extension 能很方便的处理 NSDate 对象。
NSDate时间
作者hullo-ai,源码Swift-2.3-AFDateHelper,AFDateHelper 是使用 Swift 编写的 NSDate 扩展。
#iOS-开发 面向开发人员的 iOS 框架、文章、电子书等列表。 使用生成的目录 存储库 系统 Swift 工具 Xcode 包管理器 ...Eidolon - 艺术拍卖亭应用程序。...如何在 Xcode 5 中生成漂亮的 Apple...NSDate-TKExtensions - NSDa
object-c基础语法NSDate, 适合初学者,主要学习NSDate的设置、获取当前时间、当前时间加减秒后的时间、日期比较、日期转换成NSString等
NSDate分类,获得对应中国农历
说明请看:http://blog.csdn.net/dolacmeng/article/details/51344748
NSDate扩充功能,可更容易操作NSDate ,它建立在SWIFT的顶部功能提供最好的体验和安全性。特征使用常用比较运算符比较日期: >, >=, <, <= 使用该自定义运算符获取日期之间的间隔>-< 使用算术运算符添加/...
leetcode下载 iOS-small-function ...14.NSDate-Extensions-master 15.ScrollViewDemo 16.BANetManager-master 视频上传,转码,压缩 高质量技术博客 1、 2、 3、 4、 5、 6、 7、 8、 9、 10、 11、 12、
单例封装NSDateFormatter,根据需求自由封装显示时间格式