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