- NSDate *currenttime = [NSDate Date];
- DatePicker = [[Uidatepicker alloc] initwithframe:cgrectmake (0, 100, 320, 216)];
- [DatePicker Settimezone:[nstimezone Defaulttimezone];
- [DatePicker settimezone:[nstimezone timezonewithname:@ "Gmt+8"];
- Set current display
- [DatePicker setdate:currenttime Animated:yes];
- Set Display maximum time (
- [DatePicker Setmaximumdate:currenttime];
- Display mode
- [DatePicker Setdatepickermode:uidatepickermodedateandtime];
- callback method 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);
- }
Copy Code
any one of the date range properties is not set, the default behavior will allow the user to select any past or future date. This is useful in some cases, for example, when you choose a birthday, it can be any date in the past, but terminates with the current date.
The picker's height is always 216 pixels
about NSDate: source Http://hi.baidu.com/douxinchun/item/86c9732bc603349db73263ae common usage of nsdate
1. The following methods are available for creating or initializing the class method used to create the NSDate instance has + (ID) date; return Current Time
+ (ID) datewithtimeintervalsincenownstimeinterval) secs; returns the current time base, and then the time after secs seconds
+ (ID) datewithtimeintervalsincereferencedatenstimeinterval) secs; returns the 2001/01/01 GMT as the benchmark, then secs seconds later
+ (ID) datewithtimeintervalsince1970nstimeinterval) secs; returns the 1970/01/01 GMT as the benchmark, then secs seconds later
+ (ID) distantfuture; return a certain day of the future many years later. For example, if you need a time value that is longer than the current (now) night, you can call the method. The test returned 4000/12/31 16:00:00
+ (ID) distantpast; return a day many years ago. For example, if you need a time value that is much longer than the current (now), you can call the method. The test returned 0001/12/31 BC 17:00:00
The instance method used to create the NSDate instance has -(ID) addtimeintervalnstimeinterval) secs; Returns the time that is saved in the current instance, and then secs seconds
The instance method used to initialize the NSDate instance has -(ID) init; initialized to the current time. Similar to the date method
-(ID) initwithtimeintervalsincereferencedatenstimeinterval) secs; initialized to 2001/01/01 GMT, and then secs seconds later. Similar datewithtimeintervalsincereferencedate: Method
-(ID) initwithtimeintervalnstimeinterval) secs sincedatensdate *) refdate; initialized to Refdate, then secs seconds later.
-(ID) initwithtimeintervalsincenownstimeinterval) secs; initialized to the current time, and then secs seconds later
2. Comparisons between dates are available in the following ways -(BOOL) isequaltodatensdate *) otherdate; same as Otherdate, return Yes
-(NSDate *) earlierdatensdate *) anotherdate; returns the earlier date compared to the Anotherdate
-(NSDate *) laterdatensdate *) anotherdate; compare with Anotherdate, return to the later date
-(Nscomparisonresult) comparensdate *) Other; called when the method is used for sorting: when the date value saved by the instance is returned with the anotherdate phase Nsorderedsame returns nsordereddescending when the instance holds a date value that is later than Anotherdate . Returns nsorderedascending when the instance holds a date value earlier than Anotherdate
3. The time interval can be retrieved using the following methods
-(Nstimeinterval) timeintervalsincedatensdate *) refdate; returns the time interval between the time the instance was saved and the Refdate, with Refdate as the base time
-(Nstimeinterval) Timeintervalsincenow; Returns the time between the time that the instance was saved and the current time (now) at the current time (now)
-(Nstimeinterval) timeIntervalSince1970; returns the time between the time the instance was saved and 1970/01/01 GMT, with 1970/01/01 GMT as the base time
-(Nstimeinterval) timeintervalsincereferencedate; returns the time between the time the instance was saved and 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. Representing time as a string
-(NSString *) description; the time is expressed in YYYY-MM-DD hh:mm:ss±hhmm format. where "±hhmm" represents the time zone difference between how many hours and minutes of the existence of GMT. For example, if the time zone is set in Beijing, then "±HHMM" is displayed as "+0800"
- 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];
- }
Copy Code
- 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];
Copy Code now Loctime is the time string for the specified time zone.
how to get a 24-hour system regardless of whether the user is setting a 12-hour or 24 -hour system
NSDateFormatter * formatter= [[NSDateFormatter alloc] init];
[Formattersetdateformat "Yyyy-mm-dd HH:mm:ss"];
nsstring *loctime = [formatter stringfromdate:date];
[Formatter release]
Note Here is the formatter format, if the lowercase "hh", then the time will be followed by the system settings into 12 hours or 24 hours. The uppercase "HH" is forced to be 24-hour.
original link: http://www.cnblogs.com/zhidao-chen/archive/2013/05/17/3083488.html
|