Obtain time information in NSDate, in years, months, weeks, days, hours, minutes, seconds, and milliseconds:
Note: The first method cannot obtain information in milliseconds, and the second method is used to obtain information in milliseconds.
[Cpp] 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];
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 method:
[Cpp]-(NSString *) NSDateToNSTring :( NSDate *) nsDate {
// NSString * string = [nsDate descriptionWithCalendarFormat: @ "% Y/% m/% d % H: % M: % S" timeZone: nil locale: nil]; this method has APIs. Do not use this method if you want to upload an APP.
NSDateFormatter * fmt = [[NSDateFormatter alloc] init] autorelease];
-(NSString *) NSDateToNSTring :( NSDate *) nsDate {
// NSString * string = [nsDate descriptionWithCalendarFormat: @ "% Y/% m/% d % H: % M: % S" timeZone: nil locale: nil]; this method has APIs. Do not use this method if you want to upload an APP.
NSDateFormatter * fmt = [[NSDateFormatter alloc] init] autorelease]; [cpp] // [fmt setDateFormat: @ "hh: mm: ss: SSS"];
// [Fmt setDateFormat: @ "hh: mm: ss: SSS"]; [cpp] [fmt setDateFormat: @ "yyyy/MM/dd hh: mm: ss: SSS "];
NSString * string = [fmt stringFromDate: nsDate];
Return string;
[Fmt setDateFormat: @ "yyyy/MM/dd hh: mm: ss: SSS"];
NSString * string = [fmt stringFromDate: nsDate];
Return string;
}
From the column zcl316369