IPhone: nsdata year, month, week, day, hour, minute, second acquisition and nsdatatonsstring Method
Obtain time information in nsdate, year, month, week, day, hour, minute, and 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:
-(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];
[FMT setdateformat: @ "yyyy/mm/dd hh: mm: SS"];
Nsstring * string = [FMT stringfromdate: nsdate];
Return string;
}