about 9 years ago
HI all,
If you want to convert NSString to NSDate ,then you must use NSDateFormatter to convert please follow this code .
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];
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)
var dateString = "11-11-2015" var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" var dateFromString = dateFormatter.dateFromString(dateString)
In Objective-C
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"dd-MM-yyyy"];
- NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
- NSLog(@"%@", stringDate);
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)
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)