iPhone开发--NSData年、月、星期、日、时、分、秒和毫秒获取及NSDataToNSString方法...

在 NSDate中获得时间信息,年、月、星期、日、时、分、秒和毫秒:

注:第一种方法不能获取毫秒的信息,最后利用第二种方法实现了获取毫秒的信息

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *now; NSDateComponents *comps = [[NSDateComponents alloc] init]; NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; now=[NSDate date]; comps = [calendar components:unitFlags fromDate:now]; int year=[comps year]; int week = [comps weekday]; int month = [comps month]; int day = [comps day]; int hour = [comps hour]; int min = [comps minute]; int sec = [comps second]; NSDataToNSString方法:
-(NSString *)NSDateToNSTring:(NSDate *)nsDate{ //NSString *string = [nsDate descriptionWithCalendarFormat:@"%Y/%m/%d %H:%M:%S" timeZone:nil locale:nil];此方法为是有API如果要上传APP不要使用 NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];// [fmt setDateFormat:@"hh:mm:ss:SSS"]; [fmt setDateFormat:@"yyyy/MM/dd hh:mm:ss:SSS"]; NSString *string=[fmt stringFromDate:nsDate]; return string; }
本文转载自:http://hobo86.iteye.com/blog/1013030
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值