Example
The code is as follows |
|
NSDate *currenttime = [NSDate Date]; DatePicker = [[Uidatepicker alloc] initwithframe:cgrectmake (0, 100, 320, 216)]; [DatePicker Settimezone:[nstimezone Defaulttimezone]]; [DatePicker settimezone:[nstimezone timezonewithname:@ "gmt+8"]]; Set the current display [DatePicker setdate:currenttime Animated:yes]; Set Display maximum time ( [DatePicker Setmaximumdate:currenttime]; Display mode [DatePicker Setdatepickermode:uidatepickermodedateandtime]; Method of callback because Uidatepicker is a subclass of Uicontrol, you can hook up a delegate in the notification structure of the Uicontrol class [DatePicker addtarget:self Action: @selector (datepickervaluechanged:) forcontrolevents:uicontroleventvaluechanged] ; [Self.view Addsubview:datepicker]; -(void) datepickervaluechanged: (ID) sender { NSDate *selected = [DatePicker date]; NSLog (@ "Date:%@", selected); } |
Any one of the date range properties is not set, the default behavior will allow the user to select any date in the past or future. This is useful in some cases, for example, when choosing a birthday, it can be any previous date, but terminated with the current date.
The picker's height is always 216 pixels
Common usage of NSDate
1. Create or initialize the following methods available
The class method used to create the NSDate instance has
+ (ID) date;
Returns the current time
+ (ID) Datewithtimeintervalsincenow: (nstimeinterval) secs;
Returns based on the current time and then secs seconds
+ (ID) datewithtimeintervalsincereferencedate: (nstimeinterval) secs;
Returns the benchmark with 2001/01/01 GMT and then secs seconds
+ (ID) dateWithTimeIntervalSince1970: (nstimeinterval) secs;
Returns the benchmark with 1970/01/01 GMT and then secs seconds
+ (ID) distantfuture;
Return some day in the future many years later.
For example, if you need a time value that is longer than the present (now) later, you can call the method. Test returned 4000/12/31 16:00:00
+ (ID) distantpast;
Return to a certain day many years ago.
For example, if you need a time value that is much longer than the present (now) early (small), you can call the method. The test returned 17:00:00 BC 0001/12/31.
The instance method used to create the NSDate instance has
-(ID) Addtimeinterval: (nstimeinterval) secs;
Returns the base of the time saved in the current instance, then secs seconds
The instance method used to initialize the NSDate instance has
-(ID) init;
Initialized to the current time. Similar to date method
-(ID) initwithtimeintervalsincereferencedate: (nstimeinterval) secs;
Initialized to 2001/01/01 GMT as the benchmark, then secs seconds. Similar Datewithtimeintervalsincereferencedate: Method
-(ID) Initwithtimeinterval: (nstimeinterval) secs sincedate: (NSDate *) refdate;
Initialized to Refdate as the base, then secs seconds
-(ID) Initwithtimeintervalsincenow: (nstimeinterval) secs;
Initialized to take the current time as the benchmark and then secs seconds
2. Comparisons between dates are available in the following ways
-(BOOL) Isequaltodate: (NSDate *) otherdate;
Compare with Otherdate, same return Yes
-(NSDate *) Earlierdate: (NSDate *) anotherdate;
Compare with Anotherdate, return the earlier date
-(NSDate *) Laterdate: (NSDate *) anotherdate;
Compare with Anotherdate, return the date later
-(Nscomparisonresult) Compare: (NSDate *) Other;
Called when the method is used for sorting:
. When the date value saved by the instance is returned to the anotherdate phase Nsorderedsame
. Returns nsordereddescending when the date value saved by the instance is later than anotherdate
. Returns Nsorderedascending when an instance holds a date value that is earlier than anotherdate
3. The retrieval time interval can be used in the following ways
-(Nstimeinterval) Timeintervalsincedate: (NSDate *) refdate;
Takes Refdate as the base time, returns the time interval between the instance saved and the Refdate
-(Nstimeinterval) Timeintervalsincenow;
Returns the time between the time the instance was saved and the current time (now) at the current time (now) as the base time
-(Nstimeinterval) timeIntervalSince1970;
Returns the time interval between the instance saved and the 1970/01/01 GMT with 1970/01/01 GMT as the base time
-(Nstimeinterval) timeintervalsincereferencedate;
Returns the time interval between the instance saved and the 2001/01/01 GMT with 2001/01/01 GMT as the base time
+ (Nstimeinterval) timeintervalsincereferencedate;
Returns the time interval between the current time (now) and 2001/01/01 GMT with 2001/01/01 GMT as the base time
4. Represent time as a string
-(NSString *) description;
Represents the time in the YYYY-MM-DD hh:mm:ss±hhmm format.
Where "±hhmm" represents the time zone difference between how many hours a GMT exists and how many minutes. For example, if the time zone is set in Beijing, then "±HHMM" is displayed as "+0800″
about Nstimezone:
The code is as follows |
|
nstimezone* timename = [[Nstimezone alloc] initwithname:names]; Nsarray *timezonenames = [Nstimezone knowntimezonenames]; nsdate* nowdate = [NSDate Date]; For (nsstring* names in timezonenames) { nstimezone* timename = [[Nstimezone alloc] initwithname:names]; NSDateFormatter *outputformatter = [[NSDateFormatter alloc] init]; [Outputformatter setdateformat:@ "dd/mm/yyyy HH:mm:ss"]; [Outputformatter Settimezone:timename]; NSString *newdatestring = [Outputformatter stringfromdate:nowdate]; NSLog (@ "/nzone:%@,%@", [timename name], newdatestring); [timename name],[nowdate descriptionwithcalendarformat:@ "%d/%m/%y,%h:%m:%s%z" Timezone:timename Locale:[[nsuserdefaults Standarduserdefaults] dictionaryrepresentation]); [Outputformatter release]; [Timename release]; } ---------------------------------------- NSDateFormatter *formatter= [[NSDateFormatter alloc] init]; [Formatter setdateformat:@ "Yyyy-mm-dd Hh:mm:ss"]; nstimezone* TimeZone = [Nstimezone timezonewithname:@ "Asia/shanghai"]; [Formatter Settimezone:timezone]; NSString *loctime = [Formatter stringfromdate:date]; [Formatter release]; |
Now Loctime is the time string for the specified time zone.
-----------------------------------------
How do you get a 24-hour system regardless of whether the user is setting a 12-hour or 24-hour system?
The code is as follows |
|
----------------------------------------- NSDateFormatter * formatter= [[NSDateFormatter alloc] init]; [formattersetdateformat:@ "Yyyy-mm-dd HH:mm:ss"]; NSString *loctime = [Formatter stringfromdate:date]; [Formatter release] |
Note Here is the format of the formatter, if it is lowercase "hh", then the time will follow the system settings into 12 hours or 24 hours. The capitalized "HH" is forced to be a 24-hour system.