您的位置:首页 > 移动开发 > Objective-C

Objective-c_Date 日期对象

2016-04-29 23:50 381 查看
NSDate *date = [NSDate
date];

NSLog(@"%@",date);

NSLog(@"%@",[date
descriptionWithLocale:[NSLocale
currentLocale]]);

NSDate *date1 = [NSDate
dateWithTimeIntervalSince1970:60*60*12];

NSLog(@"%@",[date1
descriptionWithLocale:[NSLocale
currentLocale]]);

NSDate *date2 = [NSDate
distantFuture];

NSLog(@"%@",date2);

NSLog(@"%@",[date2
descriptionWithLocale:[NSLocale
currentLocale]]);

NSDate *date3 = [NSDate
distantPast];

NSLog(@"%@",[date3
descriptionWithLocale:[NSLocale
currentLocale]]);

//NSDate的比较

NSDate *later= [date3
laterDate:date2];

NSLog(@"%@",[later
descriptionWithLocale:[NSLocale
currentLocale]]);

NSDate *earlier = [date3
earlierDate:date2];

NSLog(@"%@",[earlier
descriptionWithLocale:[NSLocale
currentLocale]]);

NSTimeInterval sc = [date
timeIntervalSinceNow];

NSDate *currentDate = [[NSDate
alloc]initWithTimeIntervalSinceNow:sc];

NSLog(@"%@",currentDate);

//NSDate和字符串的转换

NSDateFormatter *df = [[NSDateFormatter
alloc]init];

df.dateFormat =
@"yyyy-MM-dd hh:mm:ss";

//[df setDateFormat:@"yyyy年MM月dd日
HH小时mm分ss秒"];

NSString *na = [df
stringFromDate:date];

NSLog(@"系统当前时间为:%@",na);

//取出日期中的年、月、日、时、分、秒

NSCalendarDate *calendar = [NSCalendarDate
calendarDate];

int year = [calendar
yearOfCommonEra];

int month = [calendar
monthOfYear];

int day = [calendar
dayOfMonth];

NSLog(@"%4d-%02d-%02d",year,month,day);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: