Skip to main content

Set Date And Time Picker In Objective C

Set Date picker And Time

// Set Date picker...

UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode=UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[txtDate setInputView:datePicker];

// Set Toolbar for DatePicker...

UIToolbar* dateToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
dateToolbar.barStyle = UIBarStyleBlackTranslucent;
dateToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],

[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(Done)],nil];

[dateToolbar sizeToFit];
txtDate.inputAccessoryView = dateToolbar;

// Set Time picker...

UIDatePicker *timePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[timePicker setDate:[NSDate date]];
timePicker.datePickerMode=UIDatePickerModeTime;
[timePicker addTarget:self action:@selector(updateTimeTextField:) forControlEvents:UIControlEventValueChanged];

[txtTime setInputView:timePicker];

// Set Toolbar for TimePicker...

UIToolbar* timeToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
timeToolbar.barStyle = UIBarStyleBlackTranslucent;
timeToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],

[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(Done1)],nil];

[timeToolbar sizeToFit];
txtTime.inputAccessoryView = timeToolbar;

// AutoFill Current Time...

NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"HH:mm:ss"];
NSString *formattedDate1 = [dateFormatter1 stringFromDate:[NSDate date]];
NSString *strdate=[[NSString alloc]initWithString:formattedDate1];

txtTime.text = strdate;

// Autofill current date...

NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"dd MMMM yyyy"];
NSString *formattedDate2 = [dateFormatter2 stringFromDate:[NSDate date]];
NSString *strdate1=[[NSString alloc]initWithString:formattedDate2];

txtDate.text = strdate1;
}

-(void)Done1
{
        if (txtTime.text.length == 0)
        {
              NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
              [dateFormatter1 setDateFormat:@"HH:mm:ss"];
              NSString *formattedDate1 = [dateFormatter1 stringFromDate:[NSDate date]];
              NSString *strdate=[[NSString alloc]initWithString:formattedDate1];

              txtTime.text = strdate;
         }

    [txtTime resignFirstResponder];
}

-(void)Done
{
      if (txtDate.text.length == 0)
      {
             NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
             [dateFormatter1 setDateFormat:@"dd MMMM yyyy"];
             NSString *formattedDate1 = [dateFormatter1 stringFromDate:[NSDate date]];
             NSString *strdate=[[NSString alloc]initWithString:formattedDate1];

             txtDate.text = strdate;
      }
    [txtDate resignFirstResponder];
}

// UITextField Delegate Method...

-(void)updateTextField:(id)sender
{
        UIDatePicker picker = (UIDatePicker)txtDate.inputView;
        NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
        [dateFormatter1 setDateFormat:@"dd MMMM yyyy"];
        NSString *formattedDate1 = [dateFormatter1 stringFromDate:picker.date];
        NSString *strdate=[[NSString alloc]initWithString:formattedDate1];

   txtDate.text = strdate;
   }


-(void)updateTimeTextField:(id)sender
{
         UIDatePicker picker = (UIDatePicker)txtTime.inputView;
         NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
         [dateFormatter1 setDateFormat:@"HH:mm:ss"];
         NSString *formattedDate1 = [dateFormatter1 stringFromDate:picker.date];
         NSString *strdate=[[NSString alloc]initWithString:formattedDate1];

   txtTime.text = strdate;
  } 


Comments

  1. Hello, an amazing Information dude. Thanks for sharing this nice information with us. Buy Apple Developer Enterprise Account

    ReplyDelete

Post a Comment

Thank You.

Popular Posts

Master Map & Filter, Javascript’s Most Powerful Array Functions

Master Map & Filter, Javascript’s Most Powerful Array Functions Learn how Array.map and Array.filter work by writing them yourself This article is for those who have written a  for  loop before, but don’t quite understand how  Array.map  or  Array.filter  work. You should also be able to write a basic function. By the end of this, you’ll have a complete understanding of both functions, because you’ll have seen how they’re written. Array.map Array.map  is meant to transform one array into another by performing some operation on each of its values. The original array is left untouched and the function returns a new, transformed array. For example, say we have an array of numbers and we want to  multiply each number by three . We also don’t want to change the original array. To do this without  Array.map , we can use a standard for-loop. for-loop var originalArr = [1, 2, 3, 4, 5]; var newArr = []; for(var i = 0; i < originalArr.length; i+...

16 AWS Gotchas

16 AWS Gotchas In January I launched the MVP for my own startup,  Proximistyle , which helps you find what you’re looking for nearby. On advice from friends and industry contacts I chose AWS as my cloud provider. Having never had to set up my own cloud infrastructure before, the learning curve to get from no experience to a stable VPC system I was happy with was significantly steeper than expected, and had its fair share of surprises. #1 Take advantage of the free resources offered AWS offers a free tier for new accounts. If you have recently bought a domain and set up a company you qualify for the free tier for a year. Additionally, if you are a bootstrapped startup you can apply for  the Startup Builders package  and get $1000 in AWS credits. After doing the above, you’re now ready to get started with setting up the AWS infrastructure for your startup. #2 Set up billing budgets and alerting The very first thing you should do after setting up billing, is enabling a budge...

Add Custom Checkbox on UITableViewCell or UITableView in Swift 4

Add Custom Checkbox Introduction to how to add custom checkbox on UITableViewCell or UITableView in swift 4 In this tutorial, we are going to learn about how can we create or add custom checkbox on UITableViewCell or UITableView in swift and perform IBAction on our custom checkbox placed on UITableViewCell or UITableView in swift 4. We will place UIButton on UITableViewCell that will act as checkbox for our UITableViewCell in UITableView using swift 4. In the end of this tutorial, we will have output as shown in below picture Learn how to create checkbox in swift from  HERE Steps to create or add custom checkbox on UITableViewCell or UITableView Step 1 :  Create a new Xcode project and select  "Single View App"  template. Name  project as  "addCheckBoxonTable-Tutorial" . Step 2:  Open  "Main.storyboard"  file and drag UITableView object on to the view. Then we will add constraints to UITableView so that it fits...