------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! ------
Brief introduction
NSDate: an OC class for saving time values, while providing some way to handle some time-related things. the NSDate object is used to represent a specific point in time. NSDate is a class cluster, and the NSDate object that we use is the entity of the private subclass of NSDate. The NSDate stores the GMT time, which is used to calculate or display the time based on the timezone specified in the current application.
NSDate
1. Basic use of Time objects
The first method of creation (which stores the current time (time in 0 time zone-GMT)) NSDate *date1 = [NSDate date];//the second way to create (create a time, 5 seconds later than date1) nsdate *date2 = [nsdate Datewithtimeinterval:10 sincedate:date1];//the number of seconds from 1970 to date2 nstimeinterval time1 = [Date2 timeIntervalSince1970] ;//from the calling code to the current number of seconds (nstimeinterval = = Double) Nstimeinterval time2 = [Date2 timeintervalsincenow]; NSLog (@ "%lf", time2);
2. Formatting of the Time object
#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool {//The first creation method (inside Storage Current time (time in 0 time zone-GMT)) NSDate *date1 = [NSDate Date]; Convert NSString to NSDate (formatted output) nsdateformatter *formate = [[NSDateFormatter alloc]init]; MM uppercase means the month lowercase means minutes, the other lowercase formate.dateformat = @ "Yy-mm-dd hh:mm:ss"; NSString *date = [Formate stringfromdate:date1]; NSLog (@ "%@", date); Convert NSString to NSDate (this Xcode store automatically converts the set time to GMT storage) NSString *date2 = @ "1992/1/1 10:10:01"; Formate.dateformat = @ "Yy/mm/dd hh:mm:ss"; NSDate *date3 = [Formate datefromstring:date2]; NSLog (@ "%@", date3); /************ time between this time zone and the 0 time zone ************///Get the time difference between this timezone and GMT, and save it to nstimeinterval times = [[Nstimezone systemt Imezone] SECONDSFROMGMT]; time/60.0/60.0 converts the seconds stored in time to the hourly output NSLog (@ "Time difference:%f hours", time/60.0/60.0); Convert GMT to this time zone date3 = [Date3 Datebyaddingtimeinterval:time]; NSLog (@ "%@", date3); } return 0;}
Reference documents
Justben Open source China-Blog
isaced (conversion between time format and time zone)
Dark Horse programmer--foundation Framework Common Class (NSDate)