My OC Growth path (date and time of review)

Source: Internet
Author: User
Tags date1 difference between two times

Attention:

1. Calculate the calendar in the current month

Calculated for a total of several days within one months:
Gets the number of days in the month-(Nsinteger) getnumberofdaysinmonth{    nscalendar * calendar = [[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar]; The algorithm for the specified calendar    nsdate * currentdate = [NSDate date];//This date can be given yourself    nsrange range = [Calendar Rangeofunit:nsdaycalenda Runit                                   inunit:nsmonthcalendarunit                                  fordate:currentdate];    return range.length;}
Gets the number of days of the week in the month
/** * Get all days in the month is weeks */-(void) getalldayswithcalender{Nsuinteger daycount = [self getnumberofdaysinmonth];//One months total days    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];    NSDate * currentdate = [NSDate Date];    [Formatter setdateformat:@ "yyyy-mm"];    NSString * str = [formatter stringfromdate:currentdate];    [Formatter setdateformat:@ "YYYY-MM-DD"];    Nsmutablearray * Alldaysarray = [[Nsmutablearray alloc] init];        for (Nsinteger i = 1; I <= daycount; i++) {NSString * sr = [nsstring stringwithformat:@ "%@-%ld", str,i];        NSDate *sudate = [Formatter DATEFROMSTRING:SR];    [Alldaysarray addobject:[self getweekdaywithdate:sudate]; } NSLog (@ "Alldaysarray%@", Alldaysarray);} /** * Get data for one day * * Gets the date specified is the days of the week */-(ID) getweekdaywithdate: (NSDate *) date{Nscalendar * calendar = [[Nscalendar alloc ] Initwithcalendaridentifier:nsgregoriancalendar];    The algorithm for the specified calendar nsdatecomponents *comps = [Calendar Components:nsweekdaycalendarunit fromdate:date];1 is Sunday, 2 is Week 13. And so on return @ ([comps weekday]);} 

2. Compare two dates, compare: Method

Returns the Nscomparisonresult enumeration value

The enumeration type contains three values of nsorderedascending, Nsorderedsame, and nsordereddescending

3.

NSDate--Represents an absolute point in time
Nstimezone--Time zone information
Nslocale--Localization information
Nsdatecomponents--A class that encapsulates a specific date, time, week, quarter, etc.
Nscalendar--Calendar class, which provides most of the date compute interfaces and allows you to convert between NSDate and nsdatecomponents

NSDateFormatter--Used to convert between dates and strings

First, NSDate

1. Get nsdatensdate* date1 = [NSDate Date] representing the current date and time; NSLog (@ "%@", date1);//2. Gets the date from the current time, after the day, or 3 days before the date nsdate* date2 = [[NSDate alloc]initwithtimeintervalsincenow:3600* 24]; NSLog (@ "%@", date2); nsdate* date3 = [[NSDate alloc]initwithtimeintervalsincenow: -3*3600*24]; NSLog (@ "%@", date3);//3. Date from January 1, 1970, 20 years later nsdate* date4 = [nsdate datewithtimeintervalsince1970:3600 * 24 * 366 * 2 0]; NSLog (@ "%@", date4); nslocale* cn = [Nslocale Currentlocale]; NSLog (@ "%@", [Date1 DESCRIPTIONWITHLOCALE:CN]);//4. Get two dates between the earlier dates/get two dates later on the date nsdate* earlier = [Date1 earlierdate: DATE2]; nsdate* later = [Date1 laterdate:date2];switch ([date1 compare:date3]) {case Nsorderedascending:nslog (@ "Date1 before Date3 Break;case Nsorderedsame:nslog (@ "date1 and date3 date equal"); Break;case Nsordereddescending:nslog (@ "Date1 is located after Date3"); break;} 5. Get the time difference between two times/Gets the time difference between the specified and present times

NSLog (@ "Date1 and date3 time difference%g seconds", [Date1 timeintervalsincedate:date3]);
NSLog (@ "Date2 and now time difference%g seconds", [Date2 Timeintervalsincenow]);

  

Second, date format device

1. Obtain the date from January 1, 1970 onwards, 20 years after nsdate* dt = [nsdate datewithtimeintervalsince1970:3600 * 24 * 366 * 20]; nslocale* locales[] = [[Nslocale alloc] initwithlocaleidentifier:@ "ZH_CN"];nsdateformatter* df;// Create DateFormat object for the above nslocale df = [[NSDateFormatter alloc] init];//2. Set the date and time style of the NSDateFormatter [DF Setdatestyle: Nsdateformattershortstyle]; [DF settimestyle:nsdateformattershortstyle];//Set Custom Formatter template [Df2 setdateformat:@ "A.D. yyyy mm month DD day hh mm"];//3. Executes the formatted NSLog (@ "%@", [Df2 Stringfromdate:dt]); nsstring* datestr = @ "2013-03-02"; nsdateformatter* df3 = [[NSDateFormatter alloc] init];//4. Format template based on date string [df3 setdateformat:@ "Yyyy-mm-dd"];// Converts a string to a NSDate object nsdate* date2 = [Df3 datefromstring:datestr]; NSLog (@ "%@", date2);

  

Third, calendar

1. Get the Calendar object that represents the Gregorian calendar Nscalendar *gregorian = [[Nscalendar alloc]initwithcalendaridentifier:nsgregoriancalendar];// 2. Get the current date nsdate* dt = [NSDate date];//3. Defines a flag for a time field that specifies the information for the specified year, month, day, hour, minute, second, unsigned unitflags = Nsyearcalendarunit |  Nsmonthcalendarunit | Nsdaycalendarunit |  Nshourcalendarunit | Nsminutecalendarunit | Nssecondcalendarunit | nsweekdaycalendarunit;//4. Get information on different time fields nsdatecomponents* comp = [Gregorian components:unitflags fromdate:dt];//5. Gets the numeric nslog for each time field (@ "Now is%ld years", comp.year); NSLog (@ "Now is%ld months", comp.month); NSLog (@ "Now is%ld Day", Comp.day); NSLog (@ "Now is%ld", comp.hour); NSLog (@ "Now is%ld", comp.minute); NSLog (@ "Now is%ld seconds", Comp.second); NSLog (@ "Now is week%LD", comp.weekday);//6. Create a Nsdatecomponents object again nsdatecomponents* comp2 = [[Nsdatecomponents alloc] init];//7. Set the value of each time field comp2.year = 2013;comp2.month = 4;comp2.day = 5;comp2.hour = 18;comp2.minute = 34;//8. by nsdatecompon Ents contains the value of the Time field to restore the NSDate object nsdate *date = [Gregorian DATEFROMCOMPONENTS:COMP2]; NSLog (@ "Gets the date:%@", date);

  

My OC Growth path (date and time of review)

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.