iOS development get the current week program code

Source: Internet
Author: User


Converts the current date to the day of the week

-  (void) Getweek {    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:[nsdate date]

];

    int year=[comps year];

    int week = [comps weekday];     int month = [comps&nbSp;month];

    int day = [comps day];

    int hour = [comps hour];

    int min = [comps minute];

    int sec = [comps second];           NSArray * arrWeek=[NSArray 

arraywithobjects:@ "Sunday", @ "Monday", @ "Tuesday", @ "Wednesday", @ "Thursday", @ "Friday", @ "Saturday", nil];           nslog (@ "Week:%@",  [nsstring stringwithformat:@ "%

@ ", [arrweek objectatindex:[comps weekday] - 1]]); }


This method can not only get the week, but also get the current year, month, time seconds, and so on. More ways to get into the method and see for yourself.

Related Article

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.