How to convert string to date?

I have two lines: date1 = 3-5-2014; date2 = 4-2-2010;

I need to convert them to a date and then compare them. I want the same date format as the strings, i.e. dd-mm-yyyy. How can I do that?

+4
source share
4 answers
NSString * string=@ "03-05-2014"; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSDate *dateFromString = [dateFormatter dateFromString:string]; 

enjoy...

+4
source

You can extract NSDate objects from strings using the NSDateFormatter class. See Date Formats in the Apple Data Formatting Guide for a detailed explanation and sample code.

+2
source

Try below

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"MM/dd/yyyy h:mm a"]; NSDate *date = [dateFormat dateFromString:dateStr]; [dateFormat release]; 

here is the SO post

Convert NSString date to NSDate

+2
source
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSDate *date = [dateFormatter dateFromString:@"25-8-2010"]; NSLog(@"%@", date); [dateFormatter release]; 

Hope this works for you
Greetings :)

+2
source

Source: https://habr.com/ru/post/1346673/


All Articles