Summary of practical methods of objective-c time operation

Source: Internet
Author: User

Summary of practical methods of objective-c time operation

get the end date according to the month
/** *  getmonthenddate *  get the end date according to the month * *  @param monthbegin month (yyyy-mm-dd HH:mm:ss) * *  @return Nsdate/nil */+ (NSDate *) Getmonthenddate: (NSString *) monthbegin{    if (![ Monthbegin isequaltostring:@ ""]) {        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];        [Formatter Settimezone:[nstimezone timezonewithabbreviation:@ "Utc+8"];        [Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];        Nsdatecomponents *currentmonth = [[Nsdatecomponents alloc] init];        Currentmonth.month = 1;         NSDate *end = [[Nscalendar Currentcalendar] Datebyaddingcomponents:currentmonth todate:[formatter dateFromString: Monthbegin] options:0];        return end;    } else return nil;}

Get week by date
/** * Getweekfordate * Get week by date * * @param nsstring strdate Date (YYYY-MM-DD) * * @return Nsstring/empty */+ (nsstring*) Getweekfordate: (NSString *) strdate{if (![        Strdate isequaltostring:@ ""]) {//time format NSDateFormatter *formatter = [[NSDateFormatter alloc] init];        Get the corresponding time [formatter setdateformat:@ "YYYY-MM-DD"];        NSDate *cdate = [Formatter datefromstring:strdate];        Get time information [formatter setdateformat:@ "Yyyy-mm-dd eeee HH:mm:ss a"];        NSString *locationstring=[formatter Stringfromdate:cdate];        Nsarray *arrdate = [locationstring componentsseparatedbystring:@ ""];        NSString *strweek = [NSString stringwithformat:@ "%@", [Arrdate Objectatindex:1]];        if ([Strweek isequaltostring:@ "Sunday"]) return @ "Weekend";        else if ([Strweek isequaltostring:@ "Monday"]) return @ "Monday";        else if ([Strweek isequaltostring:@ "Tuesday"]) return @ "Tuesday";      else if ([Strweek isequaltostring:@ "Wednesday"]) return @ "Wednesday";  else if ([Strweek isequaltostring:@ "Thursday"]) return @ "Thursday";        else if ([Strweek isequaltostring:@ "Friday"]) return @ "Friday";        else if ([Strweek isequaltostring:@ "Saturday"]) return @ "Saturday";    else return @ ""; }else return nil;}

Gets the UNIX timestamp based on the current time

    Get timestamp    nsdate *datetime = [NSDate date];    Nstimezone *zone = [Nstimezone timezoneforsecondsfromgmt:8];    Nsinteger interval = [zone secondsfromgmtfordate:datetime];    NSDate *localedate = [datetime  datebyaddingtimeinterval:interval];    NSString *unixtime = [NSString stringwithformat:@ "%.f", Floor ([localedate timeIntervalSince1970]);

The following article provides considerable help for this article, expressing gratitude:

Http://book.2cto.com/201304/21439.html

Http://blog.sina.com.cn/s/blog_4e24d9c501010lu2.html

Summary of practical methods of objective-c time operation

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.