iOS date-related actions

Source: Internet
Author: User

Additional setup after loading the view, typically from a nib.    Get the current date note week1 is Sunday nsdate *date = [NSDate Date];        NSLog (@ "date:%@", date); Get (24 * 60 * 60) that is 24 hours before the date, datewithtimeintervalsincenow:nsdate *yesterday = [NSDate datewithtimeintervalsincenow:-(    24* 60* 60)];            NSLog (@ "yesterday:%@", yesterday);    NSDateFormatter *formatter =[[nsdateformatter alloc] init];    NSDate *date_ = [NSDate Date];    [Formatter Settimestyle:nsdateformattermediumstyle];        Nscalendar *calendar = [[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar];    Nsdatecomponents *comps = [[Nsdatecomponents alloc] init];    Nsinteger unitflags = Nsyearcalendarunit |    Nsmonthcalendarunit |    Nsdaycalendarunit |    Nsweekdaycalendarunit |    Nshourcalendarunit |    Nsminutecalendarunit |        Nssecondcalendarunit;    int Week=0;week1 is Sunday, Week7 is Saturday;    Comps = [Calendar Components:unitflags fromdate:date];    Int week = [comps weekday]; INT year=[comps year];    int month = [Comps month];        int day = [Comps day];        [Formatter Setdatestyle:nsdateformattermediumstyle];        This sets the label with the updated time.    int hour = [Comps hour];    int min = [comps minute];    int sec = [comps second];    NSLog (@ "week:%d:%@", week,[self Week:week]);    NSLog (@ "year:%d", year);    NSLog (@ "month:%d", month);    NSLog (@ "day:%d", day);    NSLog (@ "hour:%d", hour);    NSLog (@ "min:%d", min);        NSLog (@ "sec:%d", sec);    Get millisecond NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];    [Dateformatter Setdatestyle:nsdateformattermediumstyle];    [Dateformatter Settimestyle:nsdateformattershortstyle]; [Dateformatter setdateformat:@ "Hh:mm:ss"] [dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss.    SSS "]; NSLog (@ "date:%@", [Dateformatter stringfromdate:[nsdate Date]]);


-(nsstring*) Week: (Nsinteger) week{    Nsstring*weekstr=nil;    if (week==1)    {        [email protected] "Sunday";    } else if (week==2) {        [email protected] "Monday";    } else if (week==3) {        [email protected] "Tuesday";    } else if (week==4) {        [email protected] "Wednesday";    } else if (week==5) {        [email protected] "Thursday";    } else if (week==6) {        [email protected] "Friday";    } else if (week==7) {        [email protected] "Saturday";    }    return WEEKSTR;}

byNSDateconverted toNSString:

NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [Dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"]; NSString *strdate = [dateformatter stringfromdate:[nsdate Date]]; NSLog (@ "%@", strdate);


by NSString conversion to NSDate:

NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [Dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"]; NSDate *date = [dateformatter datefromstring:@ "2010-08-04 16:01:03"]; NSLog (@ "%@", date);


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.