NSDate
//得到当前的日期
NSDate *date = [NSDate date];
NSLog(@"date:%@",date);
//得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow:
NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)];
NSLog(@"yesterday:%@",yesterday);
NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
NSInteger unitFlags = NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSWeekdayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit;
//int week=0;
comps = [calendar components:unitFlags fromDate:date];
int week = [comps weekday];
int year=[comps year];
int month = [comps month];
int day = [comps day];
//[formatter setDateStyle:NSDateFormatterMediumStyle];
//This sets the label with the updated time.
int hour = [comps hour];
int min = [comps minute];
int sec = [comps second];
NSLog(@"week%d",week);
NSLog(@"year%d",year);
NSLog(@"month%d",month);
NSLog(@"day%d",day);
NSLog(@"hour%d",hour);
NSLog(@"min%d",min);
NSLog(@"sec%d",sec);
//得到毫秒
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
//[dateFormatter setDateFormat:@"hh:mm:ss"]
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter release];
==========================================
方法一
NSDate* toDate1 = [ [ NSDate alloc] initWithString:@"2520-9-26 17:10:00 +0600" ];
NSTimeInterval distance = [ toDate1 timeIntervalSinceNow ];
NSTimeInterval iDat = distance / ( 86400 ) ;
NSLog( @" From now to %@ diff: %f ", [toDate1 description ], iDat );
[ toDate1 release ];
方法二
NSDate* toDate = [ [ NSDate alloc] initWithString:@"2009-9-29 0:0:00 +0600" ];
NSDate* startDate = [ [ NSDate alloc] init ];
NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit | NSDayCalendarUnit
| NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:startDate toDate:toDate options:0];
NSInteger diffHour = [ cps hour ];
NSInteger diffMin = [ cps minute ];
NSInteger diffSec = [ cps second ];
NSInteger diffDay = [ cps day ];
NSInteger diffMon = [ cps month ];
NSInteger diffYear = [ cps year ];
NSLog( @" From Now to %@, diff: Years: %d Months: %d, Days; %d, Hours: %d, Mins:%d, sec:%d",
[toDate description], diffYear, diffMon, diffDay, diffHour, diffMin,diffSec );
[ toDate release ];
[ startDate release ];
[ chineseClendar release ];
NSDate
//得到当前的日期 www.2cto.com
NSDate *date = [NSDate date];
NSLog(@"date:%@",date);
//得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow:
NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)];
NSLog(@"yesterday:%@",yesterday);
NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
NSInteger unitFlags = NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSWeekdayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit;
//int week=0;
comps = [calendar components:unitFlags fromDate:date];
int week = [comps weekday];
int year=[comps year];
int month = [comps month];
int day = [comps day];
//[formatter setDateStyle:NSDateFormatterMediumStyle];
//This sets the label with the updated time.
int hour = [comps hour];
int min = [comps minute];
int sec = [comps second];
NSLog(@"week%d",week);
NSLog(@"year%d",year);
NSLog(@"month%d",month);
NSLog(@"day%d",day);
NSLog(@"hour%d",hour);
NSLog(@"min%d",min);
NSLog(@"sec%d",sec);
//得到毫秒
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
//[dateFormatter setDateFormat:@"hh:mm:ss"]
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter release];
==========================================
方法一
NSDate* toDate1 = [ [ NSDate alloc] initWithString:@"2520-9-26 17:10:00 +0600" ];
NSTimeInterval distance = [ toDate1 timeIntervalSinceNow ];
NSTimeInterval iDat = distance / ( 86400 ) ;
NSLog( @" From now to %@ diff: %f ", [toDate1 description ], iDat );
[ toDate1 release ];
方法二
NSDate* toDate = [ [ NSDate alloc] initWithString:@"2009-9-29 0:0:00 +0600" ];
NSDate* startDate = [ [ NSDate alloc] init ];
NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit | NSDayCalendarUnit
| NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:startDate toDate:toDate options:0];
NSInteger diffHour = [ cps hour ];
NSInteger diffMin = [ cps minute ];
NSInteger diffSec = [ cps second ];
NSInteger diffDay = [ cps day ];
NSInteger diffMon = [ cps month ];
NSInteger diffYear = [ cps year ];
NSLog( @" From Now to %@, diff: Years: %d Months: %d, Days; %d, Hours: %d, Mins:%d, sec:%d",
[toDate description], diffYear, diffMon, diffDay, diffHour, diffMin,diffSec );
[ toDate release ];
[ startDate release ];
[ chineseClendar release ];
分享到:
相关推荐
任务它NSDate 创建和格式创建通过调用NSDateComponents构造一个具有当前日期的 NSDate 组件通过调用NSCalendar获取当前日历使用当前日历的方法dateFromComponents 将NSDate 组件更改为 NSDate 格式通过调用...
NULL 博文链接:https://o0o0o0o.iteye.com/blog/588887
Swift-NSDate-Extensions 是 Swift NSDate 扩展,帮助开发。 标签:Swift
NSDate总结,NSDateFormatter总结,NSCalender总结,倒计时(60秒倒计时,天数倒计时,cell上天数倒计时)
DateHelper - NSDate在Swift中的便利扩展
NSDate时间
object-c基础语法NSDate, 适合初学者,主要学习NSDate的设置、获取当前时间、当前时间加减秒后的时间、日期比较、日期转换成NSString等
NSDate分类,获得对应中国农历
作者hullo-ai,源码Swift-2.3-AFDateHelper,AFDateHelper 是使用 Swift 编写的 NSDate 扩展。
说明请看:http://blog.csdn.net/dolacmeng/article/details/51344748
NSDate扩充功能,可更容易操作NSDate ,它建立在SWIFT的顶部功能提供最好的体验和安全性。特征使用常用比较运算符比较日期: >, >=, <, <= 使用该自定义运算符获取日期之间的间隔>-< 使用算术运算符添加/...
NSDate,iphone显示时间,日期的方法集合
iOS时间戳字符串NSDate转化demo
单例封装NSDateFormatter,根据需求自由封装显示时间格式
一系列关于Date的小方法,包括NSDate和NSString的互相转换,大家自己看看每个方法的说明吧!
时间范围 TimeRange 提供检查 NSDate 是否发生在两个其他 NSDates 之间。 检查 NSDate 是否包含 TimeRange。(例如12:00包含10:00 ~ 20:00 ) 检查一个 TimeRange 是否与另一个 TimeRange 重叠。安装TimeRange 可...
NSDate-RFC1123 在Swift中实现HTTP日期 ,以解析类似Last-Modified的HTTP标头
NSDate-DaboExtensionNSDate分类支持Pod导入pod 'NSDate-DaboExtension', '~> 1.0.0'
iOS ,扩展NSFDate , 可以帮助开发相关
NSDate-TimeOperators 用于 NSDate 上基本比较和时间操作的 Swift 扩展 比较 这些扩展向 NSDate 添加了基本的比较运算符 <、>、<=、>=、==、!=。 所以而不是像这样的东西: firstDate.compare(secondDate)...