I generate a PDF from html source code. I read all the text line by line. I need to get all the dates from whole document. I do get all the dates using regular expression. The problem is i need to convert the string which can be in any date format(dd-MM-yyyy or yyyy-mm-dd or dd/mm/yyyy) to a specific date format (dd MMMM yyyy). I am stuck with this. Any help will be great. I need to read the date format string contains. here is my code...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileDocumentDirectorySavePath = [documentsDirectory stringByAppendingPathComponent:@"Sample.txt"];
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:fileDocumentDirectorySavePath])
[fm copyItemAtPath:fileBundlePath toPath:fileDocumentDirectorySavePath error:nil];
NSString* fileContents =
[NSString stringWithContentsOfFile:fileDocumentDirectorySavePath
encoding:NSUTF8StringEncoding error:nil];
// first, separate by new line
NSArray* allLinedStrings =
[fileContents componentsSeparatedByCharactersInSet:
[NSCharacterSet newlineCharacterSet]];
NSString* strsInOneLine;
NSArray *DateArray;
// NSString * regularExpression = @"([1-9]|0[1-9]|[12][0-9]|3[01])(st|nd|rd|th) \\w* (19|20)\\d\\d |(\\d{2}-\\d{2}-\\d{4})|(\\d{4}-\\d{2}-\\d{2})|(\\d{2}/\\d{2}/\\d{4})|(\\d{4}/\\d{2}/\\d{2})";
NSString * regularExpression = @"([1-9]|0[1-9]|[12][0-9]|3[01])(st|nd|rd|th) \\w* (19|20)\\d\\d |(\\d{2}-\\d{2}-\\d{4})|(\\d{4}-\\d{2}-\\d{2})|(\\d{2}/\\d{2}/\\d{4})|(\\d{4}/\\d{2}/\\d{2})|((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\Feb(ruary)?\\ (((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]\\d)\\d{2})";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regularExpression
options:NSRegularExpressionCaseInsensitive
error:&error];
NSString *resultString;
for (int i = 0; i < [allLinedStrings count]; i++) {
strsInOneLine = [allLinedStrings objectAtIndex:i];
DateArray = [regex matchesInString:strsInOneLine options:0 range:NSMakeRange(0, [strsInOneLine length])];
for (NSTextCheckingResult* result in DateArray)
{
resultString = [strsInOneLine substringWithRange:result.range];
NSLog(@"RESULT STRING ===== >%@",resultString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *yourDate = [dateFormatter dateFromString:resultString];
NSLog(@"NEW DATE ====== > %@",yourDate);
NSString *myDateStr;
[dateFormatter setDateFormat:@"dd MMMM yyyy"];
myDateStr = [dateFormatter stringFromDate:yourDate];
NSLog(@"NEW DATE STRING ====== > %@",myDateStr);
}
}
There are no direct way to get this. However there is a work around like this which will work in most of the cases. All you have to do is to add all the possible date formatter to the below
dateFormatterList
.Add this to your class and use it as,
Output:
02-08-2012
Javascript can do an awesome job of this and now you can call it right from Swift! (I'm sure Objective C too)
Example:
Boom! ...and an example of passing in your date from a variable: