Uidatepicker Time Picker

Source: Internet
Author: User
Tags benchmark uicontrol

  1. NSDate *currenttime = [NSDate Date];
  2. DatePicker = [[Uidatepicker alloc] initwithframe:cgrectmake (0, 100, 320, 216)];
  3. [DatePicker Settimezone:[nstimezone Defaulttimezone];
  4. [DatePicker settimezone:[nstimezone timezonewithname:@ "Gmt+8"];
  5. Set current display
  6. [DatePicker setdate:currenttime Animated:yes];
  7. Set Display maximum time (
  8. [DatePicker Setmaximumdate:currenttime];
  9. Display mode
  10. [DatePicker Setdatepickermode:uidatepickermodedateandtime];
  11. callback method because Uidatepicker is a subclass of Uicontrol, you can hook up a delegate in the notification structure of the Uicontrol class
  12. [DatePicker addtarget:self Action: @selector (datepickervaluechanged:) forcontrolevents:uicontroleventvaluechanged] ;
  13. [Self.view Addsubview:datepicker]; -(void) datepickervaluechanged: (ID) sender
  14. {
  15. NSDate *selected = [DatePicker Date]; NSLog (@ "Date:%@", selected);
  16. }
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"


  1. nstimezone* timename = [[Nstimezone alloc] initwithname:names];
  2. Nsarray *timezonenames = [Nstimezone knowntimezonenames];
  3. nsdate* nowdate = [NSDate Date];
  4. For (nsstring* names in timezonenames)
  5. {
  6. nstimezone* timename = [[Nstimezone alloc] initwithname:names];
  7. NSDateFormatter *outputformatter = [[NSDateFormatter alloc] init];
  8. [Outputformatter setdateformat:@ "dd/mm/yyyy HH:mm:ss"];
  9. [Outputformatter Settimezone:timename];
  10. NSString *newdatestring = [Outputformatter stringfromdate:nowdate];
  11. NSLog (@ "/nzone:%@,%@", [timename name], newdatestring);
  12. [timename name],[nowdate descriptionwithcalendarformat:@ "%d/%m/%y,%h:%m:%s%z"
  13. Timezone:timename
  14. Locale:[[nsuserdefaults Standarduserdefaults] dictionaryrepresentation]);
  15. [Outputformatter release];
  16. [Timename release];
  17. }
Copy Code

    1. NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
    2. [Formatter setdateformat:@ "Yyyy-mm-dd Hh:mm:ss"];
    3. nstimezone* TimeZone = [Nstimezone timezonewithname:@ "Asia/shanghai"];
    4. [Formatter Settimezone:timezone];
    5. NSString *loctime = [Formatter stringfromdate:date];
    6. [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

  

Uidatepicker Time Picker

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.