How would I convert an NSString
like \"01/02/10\" (meaning 1st February 2010) into an NSDate
? And how could I turn the NSDat
Why not add a category to NSString?
// NSString+Date.h
@interface NSString (Date)
+ (NSDate*)stringDateFromString:(NSString*)string;
+ (NSString*)stringDateFromDate:(NSDate*)date;
@end
// NSString+Date.m
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *date = [dateFormatter dateFromString:stringDate ];
[dateFormatter release];
+ (NSDateFormatter*)stringDateFormatter
{
static NSDateFormatter* formatter = nil;
if (formatter == nil)
{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
}
return formatter;
}
+ (NSDate*)stringDateFromString:(NSString*)string
{
return [[NSString stringDateFormatter] dateFromString:string];
}
+ (NSString*)stringDateFromDate:(NSDate*)date
{
return [[NSString stringDateFormatter] stringFromDate:date];
}
// Usage (#import "NSString+Date.h") or add in "YOUR PROJECT".pch file
NSString* string = [NSString stringDateFromDate:[NSDate date]];
NSDate* date = [NSString stringDateFromString:string];
NSString *dateStr = @"Tue, 25 May 2010 12:53:58 +0000";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
NSString *mystr=@"Your string date";
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [dateFormatter dateFromString:mystr];
Nslog(@"%@",now);
If you want set the format use below code:
NSString *dateString = @"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is important - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
Nslog(@"%@",[dateFormatter dateFromString:dateString]);
If anyone is interested in doing something like this in Swift these days, I have a start on something, although it's not perfect.
func detectDate(dateString: NSString) -> NSDate {
var error: NSError?
let detector: NSDataDetector = NSDataDetector.dataDetectorWithTypes(NSTextCheckingType.Date.toRaw(), error: &error)!
if error == nil {
var matches = detector.matchesInString(dateString, options: nil, range: NSMakeRange(0, dateString.length))
let currentLocale = NSLocale.currentLocale()
for match in matches {
match.resultType == NSTextCheckingType.Date
NSLog("Date: \(match.date.description)")
return match.date
}
}
return NSDate()
}
As per Swift 2.2
You can get easily NSDate from String and String from NSDate. e.g.
First set date formatter
let formatter = NSDateFormatter();
formatter.dateStyle = NSDateFormatterStyle.MediumStyle
formatter.timeStyle = .NoStyle
formatter.dateFormat = "MM/dd/yyyy"
Now get date from string and vice versa.
let strDate = formatter.stringFromDate(NSDate())
print(strDate)
let dateFromStr = formatter.dateFromString(strDate)
print(dateFromStr)
Now enjoy.