[Oc learning diary] NSDate, oc learning diary nsdate
First, we will introduce how to return date objects.
1 // obtain the current date and time 2 NSDate * now = [NSDate date]; 3 NSLog (@ "% @", now ); 4 // return the date and time 5 NSDate * mydate1970 = [NSDate dateWithTimeIntervalSince1970: 60*60] After the number of seconds (positive or negative) after January 1, 1970; 6 NSLog (@ "% @", mydate1970); 7 // return the date and time 8 NSDate * mydatenow = [NSDate dateWithTimeIntervalSinceNow: -60*60]; 9 NSLog (@ "% @", mydatenow); 10 // return the number of seconds after the specified time (positive or negative) next date and time 11 NSDate * mydatedate = [now dateByAddingTimeInterval: 60*60]; 12 NSLog (@ "% @", mydatedate ); 13 // obtain the early 14 NSDate * ear = [mydate1970 earlierDate: mydatenow]; 15 NSLog (@ "% @", ear ); 16 // obtain the 17 NSDate * last = [mydate1970 laterDate: mydatenow]; 18 NSLog (@ "% @", last) of the two latencies );
Next we will introduce how to return the number of seconds.
1 // returns the number of seconds since January 1, 1970. 2 NSTimeInterval mytime1970 = [[NSDate date] timeIntervalSince1970]; 3 NSLog (@ "% f", mytime1970 ); 4 // return the number of seconds after which the given time passes. 5 NSTimeInterval mytimenow = [mydate1970 timeIntervalSinceNow]; 6 NSLog (@ "% f", mytimenow ); 7 // obtain the time difference between the two time periods. 8 NSTimeInterval timedis = [mydate1970 timeIntervalSinceDate: mydatenow]; 9 NSLog (@ "% f", timedis );
The following describes two key usage scenarios:
1. Convert the date format to the string format
The string format is output according to the formatting object settings. yyyy is a year, and can be written to yy. The last two MM values can only be months. If only one m is written, the preceding 0 values will be omitted. If the month is a double digit, the system will automatically add the default one
A uses H 12 h for the last 24 hours in the afternoon.
The system automatically converts the time difference to you.
1 // format the date and time object and convert the event to the current time zone. 2 NSDateFormatter * dateformatter = [NSDateFormatter new]; 3 [dateformatter setDateFormat: @ "MM dd, yyyy, HH, mm, ss, a vvvv"]; 4 NSString * mystedate = [dateformatter stringFromDate: now]; 5 NSLog (@ "% @", mystedate );
2. Convert string to date format
Note: The converted date has a time difference.
1 // The formatted content of a string converted to a date object must be the same as that of a string. 2 NSString * mystrdate = @ "2012-12-12 10:12:59"; 3 NSDateFormatter * dateformatter1 = [NSDateFormatter new]; 4 [dateformatter1 setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; 5 NSDate * mystrdd = [dateformatter1 dateFromString: mystrdate]; 6 NSLog (@ "% @", mystrdd );
Next we will introduce the time zone for you.
The first is to use less content that you only need to know.
1 // System Time Zone 2 NSTimeZone * syszone = [NSTimeZone systemTimeZone]; 3 // Local Time Zone 4 NSTimeZone * localzone = [NSTimeZone localTimeZone]; 5 // Default Time Zone 6 NSTimeZone * defaultzone = [NSTimeZone defaultTimeZone]; 7 NSLog (@ "% @", syszone); 8 NSLog (@ "% @", [localzone abbreviation]); 9 NSLog (@ "% @", [defaultzone abbreviation]);
View the time zone included in the System
1 // array of all time zones in the System 2 NSArray * zonearr = [NSTimeZone knownTimeZoneNames]; 3 NSLog (@ "% @", zonearr ); 4 // Dictionary of all time zones in the system 5 NSDictionary * zonedic = [NSTimeZone abbreviationDictionary]; 6 NSLog (@ "% @", zonedic );
The last is top priority ------ time difference Conversion
1. Greenwich time zone conversion
1 // conversion time difference 2 NSString * strdate = @ "June 6, 2015 08:08:56"; 3 NSDateFormatter * formatter = [NSDateFormatter new]; 4 [formatter setDateFormat: @ "MM dd, yyyy, HH, mm, ss, second"]; 5 // set the time zone 6 [formatter setTimeZone: [NSTimeZone timeZoneForSecondsFromGMT: 0]; 7 NSDate * date = [formatter dateFromString: strdate]; 8 NSLog (@ "% @", date );
2. Set the time zone
1 // conversion time difference 2 NSString * strdate = @ "June 6, 2015 08:08:56"; 3 NSDateFormatter * formatter = [NSDateFormatter new]; 4 [formatter setDateFormat: @ "MM mm dd, yyyy hh mm mm ss seconds"]; 5 // it is best to use either of the two methods. The first and second time zones are not included and cannot be converted. 6 [formatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: @ "UTC"]; 7 [formatter setTimeZone: [NSTimeZone timeZoneWithName: @ "UTC"]; 8 NSDate * date = [formatter dateFromString: strdate]; 9 NSLog (@ "% @", date );
3. GMT time difference Conversion
1 // the time difference between the calculation system time zone and Greenwich Mean is the number of seconds 2 NSInteger timeoff = [syszone secondsFromGMT]; 3 NSLog (@ "% zi", timeoff ); 4 // conversion time difference 5 NSString * strdate = @ "June 6, 2015 08:08:56"; 6 NSDateFormatter * formatter = [NSDateFormatter new]; 7 [formatter setDateFormat: @ "MM dd, yyyy, HH, mm, ss, second"]; 8 // get the correct time when the current time is plus the time difference 9 NSDate * firdate = [formatter dateFromString: strdate]; 10 NSDate * date = [firdate dateByAddingTimeInterval: timeoff]; 11 NSLog (@ "% @", date );