IOS gets the current time and calculates the Age (difference)

Source: Internet
Author: User

Get current time

        NSDate *now = [NSDate date];        NSLog (@ "Now date was:%@", now);            Nscalendar *calendar = [Nscalendar Currentcalendar];        Nsuinteger unitflags = Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nshourcalendarunit | Nsminutecalendarunit | Nssecondcalendarunit;        nsdatecomponents *datecomponent = [Calendar components:unitflags fromdate:now];            int year = [datecomponent year];        int month = [datecomponent month];        int day = [Datecomponent day];        int hour = [Datecomponent hour];        int minute = [datecomponent minute];        int second = [datecomponent second];            NSLog (@ "Year was:%d", year);        NSLog (@ "Month is:%d", month);        NSLog (@ "Day was:%d", day);        NSLog (@ "Hour is:%d", hour);        NSLog (@ "Minute is:%d", minute);        NSLog (@ "Second is:%d", second);

Calculate age

The first is only the age of the year difference

    Calculate Age    NSString *birth = @ "1993-10-30";    NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];    [Dateformatter setdateformat:@ "Yyyy-mm-dd"];    Birthday    NSDate *birthday = [Dateformatter Datefromstring:birth];    Current time    nsstring *currentdatestr = [dateformatter stringfromdate:[nsdate Date]];    NSDate *currentdate = [Dateformatter datefromstring:currentdatestr];    NSLog (@ "currentdate%@ birthDay%@", Currentdatestr,birth);    Nstimeinterval time=[currentdate Timeintervalsincedate:birthday];    int age = ((int) time)/(3600*24*365);    NSLog (@ ' year%d ', age);

The other one draws details to the age of the day

Nscalendar *calendar = [Nscalendar currentcalendar];//defines a Nscalendar object nsdate *nowdate = [NSDate Date];    NSString *birth = @ "1900-10-30";    NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];    [Dateformatter setdateformat:@ "Yyyy-mm-dd"];        Birthday NSDate *birthday = [Dateformatter Datefromstring:birth]; Used to get the detailed time difference unsigned int unitflags = Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nshourcalendarunit | Nsminutecalendarunit |    Nssecondcalendarunit;        nsdatecomponents *date = [Calendar components:unitflags fromdate:birthday todate:nowdate options:0]; if ([date year] >0) {NSLog (@ "%@", [NSString stringWithFormat: (@ "%ld years%ld months%ld days"), (long) [date year], (long) [Date m    Onth], (long) [date Day]]); The Else if ([date month] >0) {NSLog (@ "%@", [NSString stringWithFormat: (@ "%ld month%ld Days"), (long) [date month], (long    ) [Date Day]]; } else if ([date day]>0) {NSLog (@ "%@", [NSString stringWithFormat: (@ "%ld days"),(long)    [Date Day]]);    } else {NSLog (@ "0 days"); }


string, date, int conversion

This entry: http://blog.csdn.net/tt5267621/article/details/7720434

1. String conversion to date

nsdateformatter* DateFormat = [[NSDateFormatter alloc] init];//instantiates a NSDateFormatter object
[DateFormat setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];//set the time format, where you can set the format you need
NSDate *date =[dateformat datefromstring:@ "1980-01-01 00:00:01"];

2. Convert Date to String

nsdateformatter* DateFormat = [[NSDateFormatter alloc] init];//instantiates a NSDateFormatter object
[DateFormat setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];//set the time format, where you can set the format you need
NSString *currentdatestr = [DateFormat stringfromdate:[nsdate Date]];

3. String to int

Convert NSString to int
NSString *anumberstring = @ "123";
int i = [anumberstring intvalue];

4, int to string

Convert int to NSString
int anumber = 123;
NSString *astring = [NSString stringwithformat:@ "%d", anumber];


IOS gets the current time and calculates the Age (difference)

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.