NSDate * senddate=[nsdate Date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [Dateformatter Setdateformat: @" YYYYMMDD " ]; * locationstring=[dateformatter stringfromdate:senddate]; NSLog (@ "locationstring:%@", locationstring); [Dateformatter release]; There's a problem with the wrong date.
The wording of Uncle Guan
on //Get current Time GenevaNSDate *now =[NSDate Date];GenevaNSLog (@ "Now date" is: %@ ", now);Geneva toNscalendar *calendar =[Nscalendar Currentcalendar]; .Nsuinteger unitflags = Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nshourcalendarunit | Nsminutecalendarunit |Nssecondcalendarunit; -Nsdatecomponents *datecomponent =[Calendar Components:unitflags Fromdate:now]; , the intYear =[datecomponent year];Ten intmonth =[datecomponent month]; One intDay =[Datecomponent Day]; A inthour =[Datecomponent hour]; - intminute =[datecomponent minute]; - intSecond =[datecomponent second]; the -NSLog (@ "Year is: %D ", year); -NSLog (@ "Month is: %d ", month); -NSLog (@ "Day is: %d ", day); +NSLog (@ "Hour is: %d ", hour); -NSLog (@ "minute is: %d ", minute); +NSLog (@ "second is:%d ", second);
Get the day of the week (useful) can be obtained: year, month, date, days of the week
NSDate *datet = [NSDate Date];//now timeNsarray *weekdays = [Nsarray arraywithobjects: [NSNullNULL],@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday", nil]; Nscalendar*calendars =[[Nscalendar alloc] Initwithcalendaridentifier:nscalendaridentifiergregorian]; Nstimezone*timezone = [[Nstimezone alloc] Initwithname:@"Asia/shanghai"]; [Calendars Settimezone:timezone]; Nscalendarunit Calendarunit=Nscalendarunitweekday; Nsdatecomponents*thecomponents =[calendars components:calendarunit fromdate:datet]; Nsinteger UNITFLAGSS= Nscalendarunityear |Nscalendarunitmonth|Nscalendarunitday|Nscalendarunitweekday|Nscalendarunithour|Nscalendarunitminute|Nscalendarunitsecond; Thecomponents=[calendars components:unitflagss fromdate:datet]; intWeek =[thecomponents Weekday]; intYear=[thecomponents year]; intmonth =[thecomponents Month]; intDay =[thecomponents Day]; NSLog (@"%d years%d months", Year,month); NSLog (@"%d", day); NSLog (@"xingqi%@", [weekdays ObjectAtIndex:theComponents.weekday]);
Self-study ios-get current time