Gets the current time---month, minutes, seconds------IOS

Source: Internet
Author: User

Mode one: xxxx year-xx month-xx day xx: xx minutes: XX second format

-(Ibaction) Loginaction: (UIButton *) sender

{


NSDate *date = [NSDate Date];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];


[Formatter Setdatestyle:nsdateformattermediumstyle];

[Formatter Settimestyle:nsdateformattershortstyle];

[Formatter setdateformat:@ "Yyyy-mm-dd Hh:mm:ss"];
NSString *datetime = [Formatter stringfromdate:date];
NSLog (@ "%@============ year-month-day time: minutes: Seconds =====================", DateTime);

}

Mode two: XXXX year-X month-X day XX: xx minutes: xx seconds format
-(Ibaction) Loginaction: (UIButton *) sender{

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 = (int.) [Datecomponent year];
int month = (int) [Datecomponent month];
int day = (int.) [Datecomponent Day];
int hour = (int) [Datecomponent hour];
int minute = (int) [Datecomponent minute];
int second = (int) [datecomponent second];

Nsinteger year = [datecomponent year];
Nsinteger month = [datecomponent month];
Nsinteger day = [Datecomponent day];
Nsinteger hour = [Datecomponent hour];
Nsinteger minute = [datecomponent minute];
Nsinteger second = [datecomponent second];


NSLog (@ "Year was:%ld", (long) year);
NSLog (@ "Month is:%ld", (long) month);
NSLog (@ "Day was:%ld", (long) day);
NSLog (@ "Hour is:%ld", (long) hour);
NSLog (@ "Minute is:%ld", (long) minute);
NSLog (@ "Second is:%ld", (long) second);
Conversion and splicing of strings
NSString *yearstr=[nsstring stringwithformat:@ "%ld-", (long) year];
NSString *monthstr=[nsstring stringwithformat:@ "%ld-", (long) month];
NSString *daystr=[nsstring stringwithformat:@ "%ld", (long) day];
NSString *hourstr=[nsstring stringwithformat:@ "%ld:", (long) hour];
NSString *minutestr=[nsstring stringwithformat:@ "%ld:", (long) minute];
NSString *secondstr=[nsstring stringwithformat:@ "%ld", (long) second];
String Start stitching
NSString *allstr=[yearstr STRINGBYAPPENDINGSTRING:MONTHSTR];
NSString *allstr1=[allstr STRINGBYAPPENDINGSTRING:DAYSTR];
NSString *allstr2=[allstr1 STRINGBYAPPENDINGSTRING:HOURSTR];
NSString *ALLSTR3=[ALLSTR2 STRINGBYAPPENDINGSTRING:MINUTESTR];
NSString *DATETIME=[ALLSTR3 STRINGBYAPPENDINGSTRING:SECONDSTR];
NSLog (@ "Last month days and minutes and seconds stitching results =====%@", DateTime);
}



Gets the current time---month, minutes, seconds------IOS

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.