Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Convert NSString to NSDate and vice versa

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.36k
    Comment on it

    HI all,

    If you want to convert NSString to NSDate ,then you must use NSDateFormatter to convert please follow this code .

    NSString to NSDate

    In Objective-C :-

    1. NSString *dateString = @"11-11-2015";
    2. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    3.  
    4. [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    5. NSDate *dateFromString = [[NSDate alloc] init];
    6.  
    7. dateFromString = [dateFormatter dateFromString:dateString];

    In Swift

    1. var dateString = "11-11-2015"
    2. var dateFormatter = NSDateFormatter()
    3. dateFormatter.dateFormat = "dd-MM-yyyy"
    4. var dateFromString = dateFormatter.dateFromString(dateString)

    NSDate to NSString

    In Objective-C

    1. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    2. [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    3. NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
    4. NSLog(@"%@", stringDate);

    In Swift

    1. var formatter: NSDateFormatter()
    2. formatter.dateFormat = "dd-MM-yyyy"
    3. let stringDate: String = formatter.stringFromDate(NSDate())
    4. println(stringDate)

    you can use any kind of format of date like if you want to see month first three letters then use MMM instead of MM.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: