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

React Native: How To Open App Settings Page

  React Native: How To Open App Settings Page React Native has a Linking module to manage the interaction with other applications in the device. In this article, I will explain how you can open “Application Setting Page” of the device using. Sample Case Before I explain how to do this, I’ll talk about an example case where we might need this: You need to get location permission within the application from a user using an iOS operating system and you asked for permission. But user did not agree to grant this permission the first time. In this case, you cannot ask your user for location permission the second time. In this case, the only thing to do is to direct the user to the settings page of our application and make it manually allow it. Now that we have explained our example situation, we can move on to how we can redirect our user to the “settings page of our application”. Open App Settings Page iOS If our user is using an iOS operating system, we can handle the whole process wit...

Integrate the FaceID or TouchID Authentication in iOS Apps - Swift

FaceID or TouchID Authentication Dear Folks, today I am going to explain the proper way to integrate the Face ID or Touch ID in iOS App. Face ID and Touch ID are secure, familiar authentication methods that people trust. Follow below steps: Step 1: Add NSFaceIDUsageDescription Key in info.plist file <key>NSFaceIDUsageDescription</key> <string>This app requires Face ID permission to authenticate using Face recognition.</string> Step 2: Create a swift file with any name but it would be better to related to its functionality, Like BioMetrixFile.swift Step 3: In BioMetrixFile write below codes and follow instructions. But before proceeding further import LocalAuthentication class in BioMetrixFile. Now, create typealias for success or failure blocks to well manage Authentication Success or Failure. public typealias AuthenticationSuccess = (() -> ()) public typealias AuthenticationFailure = ((AuthenticationError) -> ()) Now written all s...

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...