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.32k
    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 :-

    NSString *dateString = @"11-11-2015";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSDate *dateFromString = [[NSDate alloc] init];
    
    dateFromString = [dateFormatter dateFromString:dateString];
    

    In Swift

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

    NSDate to NSString

    In Objective-C

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

    In Swift

    var formatter: NSDateFormatter()
    formatter.dateFormat = "dd-MM-yyyy"
    let stringDate: String = formatter.stringFromDate(NSDate())
    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
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: