/*the role of NSDateFormatter 1.NSString, NSDate 2.NSDate, NSString*/voidfmt_date_to_string ();voidfmt_string_to_date ();voidFmt_string_to_date2 ();voidfmt_timestamp_to_date3 ();intMainintargcConst Char*argv[]) {@autoreleasepool {//2015-10-01 14:38:40NSDateFormatter *fmt =[[NSDateFormatter alloc] init]; Fmt.dateformat=@"YYYY-MM-DD HH:mm:ss"; NSDate*createdatdate = [FMT datefromstring:@"2015-9-15 23:59:59"]; Nscalendar*calendar =[Nscalendar Currentcalendar]; NSLog (@"%d", [calendar isdateintomorrow:createdatdate]); } return 0;}voidcalendar_interval_between_date () {//Get system Current timeNSDate *nowdate =[NSDate Date]; //the time string returned by the serverNSString *createdatstring =@"2014-02-20 10:49:54"; //Create a date formatting objectNSDateFormatter *fmt =[[NSDateFormatter alloc] init]; //Set Date formatFmt.dateformat =@"YYYY-MM-DD HH:mm:ss"; //NSString-NSDateNSDate *createdatdate =[FMT datefromstring:createdatstring]; //calculate the time interval for createdatdate and nowdateNscalendar *calendar =[Nscalendar Currentcalendar]; Nscalendarunit Unit= Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute |Nscalendarunitsecond; Nsdatecomponents*cmps = [Calendar components:unit fromdate:createdatdate todate:nowdate options:0]; NSLog (@"%@", cmps);}/** * Get all the date elements for a NSDate object: Month Day time seconds*/voidget_components_of_date () {//Get system Current timeNSDate *nowdate =[NSDate Date]; //Calendar ObjectNscalendar *calendar =[Nscalendar Currentcalendar]; Nscalendarunit Unit= Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute |Nscalendarunitsecond; Nsdatecomponents*cmps =[Calendar Components:unit fromdate:nowdate]; NSLog (@"%@", CMPS); //nsinteger year = [Calendar Component:nscalendarunityear fromdate:nowdate]; //Nsinteger minute = [Calendar Component:nscalendarunitminute fromdate:nowdate]; //NSLog (@ "%zd%zd", year, minute); //NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; // //Set Date format//Fmt.dateformat = @ "HH"; //nsstring *string = [FMT stringfromdate:nowdate]; //NSLog (@ "%@", string);}/** * Time difference between 2 nsdate*/voidDate_interval () {//Get system Current timeNSDate *nowdate =[NSDate Date]; //the time string returned by the serverNSString *createdatstring =@"2015-10-16 10:49:54"; //Create a date formatting objectNSDateFormatter *fmt =[[NSDateFormatter alloc] init]; //Set Date formatFmt.dateformat =@"YYYY-MM-DD HH:mm:ss"; //NSString-NSDateNSDate *createdatdate =[FMT datefromstring:createdatstring]; //calculates the number of seconds between Createdatdate and NowdateNstimeinterval interval =[Nowdate timeintervalsincedate:createdatdate]; NSLog (@"%f", Interval); //3600/60 =//60/60 = 1}/** * Timestamp-nsdate*/voidFmt_timestamp_to_date3 () {//Timestamp : The number of milliseconds to experience from January 1, 1970 0:0 0 secondsNsinteger timestamp =1114548435435; NSDate*date = [NSDate datewithtimeintervalsince1970:timestamp/1000.0]; NSLog (@"%@", date);}/** * NSString-NSDate*/voidFmt_string_to_date2 () {//the time string returned by the serverNSString *string=@"Tue 17:46:55 +0800"; //Create a date formatting objectNSDateFormatter *fmt =[[NSDateFormatter alloc] init]; //Parse date string in European and American format, set language type to en_USFmt.locale = [[Nslocale alloc] Initwithlocaleidentifier:@"en_US"]; //Set Date formatFmt.dateformat =@"EEE MMM dd HH:mm:ss ZZZZ yyyy"; NSDate*date = [FMT datefromstring:string]; NSLog (@"%@", date);}/** * NSString-NSDate*/voidfmt_string_to_date () {//the time string returned by the serverNSString *string=@"2015-10-16 10:49:54"; //Create a date formatting objectNSDateFormatter *fmt =[[NSDateFormatter alloc] init]; //Set Date formatFmt.dateformat =@"YYYY-MM-DD HH:mm:ss"; NSDate*date = [FMT datefromstring:string]; NSLog (@"%@", date);}/** * NSDate-NSString*/voidfmt_date_to_string () {//Get system Current timeNSDate *now =[NSDate Date]; //Create a date formatting objectNSDateFormatter *fmt =[[NSDateFormatter alloc] init]; //Set Date format: October 10, 2015 15:56 30 seconds//Year: Y//Monthly (month): M//Day: D//time (hour): H (24-hour) \h (12-hour system)//minutes (minute): M//seconds (second): s//timezone (Zone): ZFmt.dateformat=@"yyyy mm month dd Day hh mm min ss sec"; //NSDate-NSStringNSString *nowstring =[FMT Stringfromdate:now]; NSLog (@"----- %@", nowstring);}
NSDate formatting and mutual transfer