Fork me on GitHub

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;  
}

 

 

 

 

NSCalendar *gregorian = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];

    NSDate *date = [NSDate date];

    NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];

    [gregorian release];

    NSLog(@"%02i:%02i:%02i",[dateComponents hour],[dateComponents minute],[dateComponents second]);

 

 

 

 

 

  CFGregorianDate currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem());

    NSLog(@"%02i,%02i,%02.0f,",currentDate.hour,currentDate.minute,currentDate.second);

 

 

http://stackoverflow.com/questions/1338557/nsdate-datewithnaturallanguagestring-on-iphone

NSDate -dateWithNaturalLanguageString: on iPhone?

In the iPhone docset for NSDate, in the discussion area they discuss -dateWithNaturalLanguageString:locale:, however they don't document the method elsewhere on the page.

I've used the method before for iPhone and it worked just fine, although I got warnings. Now that I'm building with -Werror (which I should have been doing all along ^_^) I've run into a warning with this.

How would I replace the following lines of code?

NSDate*today =[NSDate dateWithNaturalLanguageString:@"today at 23:59:59"];
NSDate*tomorrow =[NSDate dateWithNaturalLanguageString:@"tomorrow at 23:59:59"];




answer:

One way to do this is with NSCalendar and NSDateComponents.

To get today at 23:59:59:


NSDate*now =[NSDate date];
NSCalendar*calendar =[NSCalendar currentCalendar];

NSDateComponents*comps =[calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|  NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit 
                                                                          fromDate:date];

[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];

NSDate*today =[calendar dateFromComponents:comps];

To get tomorrow at 23:59:59:


NSDate *now = [NSDate dateWithTimeIntervalSinceNow:24 * 60 * 60]; // 24h from now
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit 
                                                                          fromDate:date];

[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];

NSDate *tomorrow = [calendar dateFromComponents:comps];

Let's assume:

static const unsigned long seconds_per_day = 60 * 60 * 24;

You can get now and now+24 hours with:

NSDate* today = [NSDate date]; // set for today
NSDate* tomorrow = [today addTimeInterval:seconds_per_day];

In order to get midnight for the given day, it's important to remember that NSDate* is toll-free bridged to CFDateRef, which provides some additional APIs you'll want to use. In particular, you can convert an NSDate to a CFAbsoluteTime with CFDateGetAbsoluteTime:


CFAbsoluteTime abstime = CFDateGetAbsoluteTime(reinterpret_cast<CFDateRef>(myNSDate));
long long      inttime = static_cast<long long>(abstime);

inttime = (inttime / seconds_per_day) * seconds_per_day; // clips the time to midnight

NSDate* myNSDateAtMidnight = reinterpret_cast<NSDate*>(CFDateCreate(NULL,
                                                       static_cast<CFAbsoluteTime>(inttime)));

... which you can wrap into a function to round both today and tomorrow to midnight.

 






posted on 2012-03-27 12:31  pengyingh  阅读(4004)  评论(0编辑  收藏  举报

导航