[Code Note] compared with the current time, note the current time

Source: Internet
Author: User
Tags date1

[Code Note] compared with the current time, note the current time

Code:

# Import "RootViewController. h "@ interface RootViewController () @ end @ implementation RootViewController-(id) initWithNibName :( NSString *) bundle :( NSBundle *) handle {self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self) {// Custom initialization} return self;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. // obtain the NSLog format of the specified date (@ "--- % @ ----", [self changeTheDate: @ "2013-04-18" ByDateType: @ "yyyy-M-dd"]); // The day of the week NSLog (@ "--- % @ ---", [self getTheWeekDay: @ "2013-04-18"]); // the week of the year, the day of the week, compare whether two days are the same week NSDate * today = [NSDate date]; NSLog (@ "-- % ld ---", (long) [self didWeekOfYear: today]); NSLog (@ "--- % ld ---", (long) [self didWeekDay: today]); NSLog (@ "--- % ld ---", (long) [self didSameWeek: today date: today]) ;}# pragma-mark-get the format of the specified date/* Get the string representation of the specified date format paramDate: date: "" paramDateType: format of the date to be converted: "yyyy-M-dd" */-(NSString *) changeTheDate :( NSString *) paramDate ByDateType :( NSString *) paramDateType {// convert a date to the desired format yyyy-MM-dd TO yyyy-M-dd NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @ "yyyy-MM-dd"]; NSDate * tempDate = [dateFormatter dateFromString: paramDate]; [dateFormatter setDateFormat: paramDateType]; NSString * theDate = [dateFormatter stringFromDate: tempDate]; NSString * result = [[NSString alloc] initWithString: theDate]; return result;} # pragma-mark-day of the week/* day of the week */-(NSString *) getTheWeekDay :( NSString *) paramDate {NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @ "yyyy-M-dd"]; NSDate * theDate = [dateFormatter dateFromString: paramDate]; NSString * result = nil; NSInteger theWeek = [self didWeekDay: theDate]; switch (theWeek) {case 1: result = @ "Sunday"; break; case 2: result = @ "Monday"; break; case 3: result = @ "Tuesday"; break; case 4: result = @ "Wednesday"; break; case 5: result = @ "Thursday"; break; case 6: result = @ "Friday"; break; case 7: result = @ "Saturday"; break; default: break;} return result ;}# pragma-mark-the day of the week // obtain the day of the week-(NSInteger) didWeekDay :( NSDate *) date {NSCalendar * gregorian = [[NSCalendar alloc] parts: Unknown]; NSDateComponents * weekdayComponents = [gregorian components :( NSDayCalendarUnit | interval) fromDate: date]; NSInteger tempweekday = [weekdayComponents weekday]; return tempweekday;} # pragma-mark-the week number Of The Year // obtain the week number of the year as the current time-(NSInteger) didWeekOfYear :( NSDate *) date {NSInteger unitFlags = Beijing; NSCalendar * gregorian = [NSCalendar currentCalendar]; NSDateComponents * weekdayComponents = [gregorian components: unitFlags fromDate: date]; NSInteger weekOfYear = [weekdayComponents weekOfYear]; return weekOfYear;} # pragma-mark-determine whether it is a week // determine whether it is the same week-(BOOL) didSameWeek :( NSDate *) date1 date :( NSDate *) date2 {NSInteger week1 = [self didWeekOfYear: date1]; NSInteger week2 = [self didWeekOfYear: date2]; return week1 = week2;} @ end

 

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.