IOS Get current time

Source: Internet
Author: User

First-you can build a category:

//

Nsstring+datestr.m

get current time Demo

//

Created by Yons on 15/7/16.

Copyright (c) year yons. All rights reserved.

//


#import "Nsstring+datestr.h"

#import <CommonCrypto/CommonDigest.h>



@implementation NSString (DATESTR)


----- weeks ---

+ (nsstring*) weekdaystringfromdate: (nsdate*) inputdate {

   //Nsarray *weekdays = [Nsarray arraywithobjects: [NSNull null], @ " Sunday ", @ " Monday ", @ " Tuesday ", @ " Wednesday ", @ " Thursday ", @ " Friday ", @ " Saturday ", nil];

     nsarray *weekdays = @[ @ " Sunday " , @ " Monday " @ " Tuesday " @" " @ " Thursday " @ " Friday " @ " Saturday " ] ;

nscalendar *calendar = [[nscalendaralloc] Initwithcalendaridentifier:Nscalendaridentifiergregorian];

nstimezone *timezone = [[nstimezonealloc] initwithname:@ "Asia/shanghai" ];

[Calendarsettimezone: TimeZone];

nscalendarunit calendarunit = nscalendarunitweekday;

//nsdatecomponents *thecomponents = [Calendar Components:calendarunit fromdate:inputdate];

nsdatecomponents *thecomponents = [[nscalendarAutoupdatingcurrentcalendar] components: Calendarunit fromdate: inputdate];

NSLog (@ "%@", [nsstringstringwithformat:@ "%i", (int) thecomponents. Weekday]);

//return [weekdays ObjectAtIndex:theComponents.weekday];

//return [nsstring stringwithformat:@ "%i", (int) thecomponents.weekday];

nsstring *str = [nsstringstringwithformat:@ "%@", [weekdaysObjectatindex :(int) thecomponents. Weekday -1]];

return str;

}


// clear whitespace characters in a string

-(nsstring*) trimstring

{

return [selfstringbytrimmingcharactersinset: [ Nscharactersetwhitespaceandnewlinecharacterset]];

}


+ (nsstring *) Monthdayfromestr: (nsinteger) Inter

{



nsstring *str = [nsstringstringwithformat:@ "%ld", Inter];

return str;

}


// Calculate the date of the month

+ (nsstring *) datefromestring: (nsdate *) Date

{

Nscalendar *calendar = [nscalendarCurrentcalendar];

nsuinteger unitflags =nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute | Nscalendarunitsecond;

nsdatecomponents *datecomponent = [Calendar components: unitflags fromdate:d ate];

//nsinteger year = [datecomponent year];

nsinteger month = [datecomponent month];

nsinteger day = [datecomponent Day];

//nsinteger hour = [Datecomponent hour];

//nsinteger minute = [datecomponent minute];

//nsinteger second = [datecomponent second];

//nsinteger week = [datecomponent weekday];


nsstring *str = [nsstringstringwithformat:@ "%ld month %ld Day ", Month,day];

return str;


}


@end


Build a controller that is easy to use----

nsstring *sixstr = [nsstringdatefromestring: sixdate]; // a few months .

nsstring *sixweek = [nsstringweekdaystringfromdate: sixdate]; // weeks






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS Get current time

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.