73501

Objective C - create NSDate from two Strings

Question:

Short version: Is there a way to combine two NSDates (which can be formed from two different strings) into a single NSDate object?

Detail: In my app, there are two textfields where the user enters the day and the time, respectively (these are entered using a customised Date Picker). When I want to save the information in the database, the property of the object expects a single date, comprised of both the day and the time.

Here is how I set the strings:

if ([mode isEqualToString:@"date"]) { self.dateFormat = [[NSDateFormatter alloc] init]; [self.dateFormat setDateFormat:@"MM/dd/yy"]; self.dateTextField.text = [self.dateFormat stringFromDate:date]; } else if ([mode isEqualToString:@"time"]) { self.timeFormat = [[NSDateFormatter alloc] init]; [self.timeFormat setDateFormat:@"hh:mm a"]; self.timeTextField.text = [self.timeFormat stringFromDate:date]; }

where date is a NSDate passed in to the method. Now when I am trying to save the information in a different method, I have access to the strings and the date formats, so I am able to do

NSDate* tempDate = [self.dateFormat dateFromString: self.dateTextField.text]; NSDate* tempTime = [self.timeFormat dateFromString: self.timeTextField.text];

but that's as far as I can get... I'm not sure how to combine the dates together into a single entity. Would I combine the DateFormatter strings together somehow?

Thank you for your help :)

Answer1:

Try this.

NSString *strTemp = [NSString stringwithFormat:@"%@ %@",self.dateTextField.text,self.timeTextField.text]; NSDateFormatter *dateFotmatter = [[[NSDateFormatter alloc] init] autorelease]; [self.dateFotmatter setDateFormat:@"MM/dd/yy hh:mm a"]; NSDate* tempDate = [self.dateFotmatter dateFromString:strTemp];

Hope it helps.

Enjoy coding.

Recommend

  • In shared classes how to use local resources?
  • How do you make a custom error page?
  • Transposing a javascript array efficiently
  • iPhone UIImagePicker Camera in Landscape Orientation
  • Type mismatch: cannot convert from Connection to Connection
  • Drag and Drop for imageview not working
  • A simple datepicker in VueJS
  • UIPickerView without IB?
  • Cannot save model when using ember render helper
  • HttpListener.IsSupported is false on XP SP3
  • C#: Import/Export Settings into/from a File
  • How to get data from **Realm database** using **date object**?
  • Saving image to sd with current date and time in name doesn't work
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Combining two different ActiveRecord collections into one
  • How can I set a binding to a Combox in a UserControl?
  • Stop Bash Script if Hive Fails
  • how to save the state in userdefaults of accessory checkmark-iphone
  • AJAX Html Editor Extender upload image appearing blank
  • Why querying a date BC is changed to AD in Java?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Database structure design with variable amounts of fields
  • Recording logins for password protected directories
  • Illegal mix of collations for operation for date/time comparison
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • How would I use PHP exceptions to define a redirect?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Release, debug version and Authorization Google?
  • Web-crawler for facebook in python
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Hits per day in Google Big Query
  • Checking variable from a different class in C#
  • How to get NHibernate ISession to cache entity not retrieved by primary key