Self-study ios-get current time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.