NSDate

来源:互联网 发布:代购app软件排行 编辑:程序博客网 时间:2024/06/06 20:05
  1. //得到当前的日期     
  2.  NSDate *date = [NSDate date];     
  3.  NSLog(@"date:%@",date);     
  4.       
  5.  //得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow:     
  6.  NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)];     
  7.  NSLog(@"yesterday:%@",yesterday);     
  8. NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];     
  9.  NSDate *date = [NSDate date];     
  10.  [formatter setTimeStyle:NSDateFormatterMediumStyle];     
  11.  NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];     
  12.  NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];     
  13.  NSInteger unitFlags = NSYearCalendarUnit |      
  14.                        NSMonthCalendarUnit |     
  15.                        NSDayCalendarUnit |      
  16.                        NSWeekdayCalendarUnit |      
  17.                        NSHourCalendarUnit |     
  18.                        NSMinuteCalendarUnit |     
  19.                        NSSecondCalendarUnit;     
  20.  //int week=0;     
  21.  comps = [calendar components:unitFlags fromDate:date];     
  22.  int week = [comps weekday];      
  23.  int year=[comps year];      
  24.  int month = [comps month];     
  25.  int day = [comps day];     
  26.  //[formatter setDateStyle:NSDateFormatterMediumStyle];     
  27.  //This sets the label with the updated time.     
  28.  int hour = [comps hour];     
  29.  int min = [comps minute];     
  30.  int sec = [comps second];     
  31.  NSLog(@"week%d",week);     
  32.  NSLog(@"year%d",year);     
  33.  NSLog(@"month%d",month);     
  34.  NSLog(@"day%d",day);     
  35.  NSLog(@"hour%d",hour);     
  36.  NSLog(@"min%d",min);     
  37.  NSLog(@"sec%d",sec);     
  38. //得到毫秒     
  39. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];     
  40.  [dateFormatter setDateStyle:NSDateFormatterMediumStyle];     
  41.  [dateFormatter setTimeStyle:NSDateFormatterShortStyle];     
  42.  //[dateFormatter setDateFormat:@"hh:mm:ss"]     
  43.  [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];     
  44.  NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);     
  45.  [dateFormatter release];   
0 0