date from string not working - objective-c

CODE:
NSString* mydate = #"11/06/2012 19:15:40";
NSLog(#"%#", mydate);
NSDateFormatter* dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = #"MM-dd-yyyy HH:mm:ss";
NSDate* adate = [dateFormatter1 dateFromString:mydate];
NSLog(#"%#", adate);
LOG:
11/06/2012 19:15:40
(null)
Why am I getting null?

Your date string is using slashes but your format specifier is setup to look for dashes.

Related

UIDatePicker Order Format

I am new to ios development tried UIDatepicker format like day month date. I tried alot but not able to get, Can any one help ?
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]objectForKey:#"DatePickerViewController.selectedDate"];
storedDate = [dateFormat dateFromString:#"EE,MMMM dd"];
// add this check and set
if (storedDate == nil) {
storedDate = [NSDate date];
}
[self.pickerView setDate:storedDate animated:NO];
i need the above image format
First, get the saved date in string Format.
NSString *storedDate = [[NSUserDefaults standardUserDefaults]objectForKey:#"DatePickerViewControllerselectedDate"];
Then get the format in which the date is stored so that you can convert it to date.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat=#"EEEE, MMMM dd";
Now you can convert the stored-date to date format and set in datepicker.
NSDate *sortedDateFormatted = [format dateFromString:storedDate];
[self.pickerView setDate:sortedDateFormatted animated:NO];
You need the date format ryt, Just try this
NSDate *now = [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat=#"EEEE, MMMM dd";
NSString * dateStr = [[format stringFromDate:now] capitalizedString];
NSDate *sortedDateFormatted = [format dateFromString:dateStr];
[self.pickerView setDate:sortedDateFormatted animated:NO];

objective-c NSDateFormatter date from string returns null

I'm trying to convert this string #"August 8, 2013" to a NSDate but I keep getting NULL as my result:
strToConvert = #"August 8, 2013";
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/YYYY zzz"];
NSDate* myDate = [dateFormatter dateFromString:strToConvert];
Am I formatting it incorrectly?
Edit:
I changed the method to this:
- (NSDate*) convertStringToDate : (NSString*) strToConvert {
NSError* error = nil;
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error];
NSArray* arrDateMatches = [detector matchesInString:strToConvert options:0 range:NSMakeRange(0, [strToConvert length])];
for (NSTextCheckingResult* match in arrDateMatches) {
strToConvert = [self convertDateToString:match.date];
}
NSDate* myDate = [dateFormatter dateFromString:strToConvert];
return myDate;
}
and it works fine. The only issue is I am getting a implicit conversion from enumeration type 'enum NSTextCheckingType'... any idea of how to fix that warning?
The NSDateFormatter has the incorrect format. You need to match the format of the expected input:
[dateFormatter setDateFormat:#"MMMM d, yyyy"];

Recover a date with SQLite

I'm trying to recover a date with SQLite on iOS.
This date (saved in selectedDate) is a variable NSDate. I convert NSDate to NSString (saved in dateString).
//Code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"yyyyMMdd"];
NSString *dateString = [formatter stringFromDate:selectedDate];
[formatter release];
NString *sentence = (#"SELECT description, date_from FROM Tasks WHERE date_from = %#", dateString);
//Warning: Expression result unused :(
If I put a default date, for example: SELECT... WHERE date_from = '20120719', it runs. How can I solve it?
Sorry for my English ;) Thanks!!!
have you try LIKE is place of = ?
date_from LIKE '20120719'
and
NSString *sentence = [NSString stringWithFormat:#"SELECT description, date_from FROM Tasks WHERE date_from = %#", dateString];

Localized date (month and day) and time with NSDate

I want to be able to get the local date and time for wherever my app is run, based on the iPhone configuration. Specifically I need the date to be of format mm/dd or dd/mm (or dd.mm, mm.dd, dd-mm, mm-dd, etc) depending on the locale, and time is hh:mm. Is this possible with some combination of SDK methods?
Thanks!
I have modified the code so that it just takes the date and time out of the NSDate object with no changes:
NSDate* date = [NSDate date];
NSString* datePart = [NSDateFormatter localizedStringFromDate: date
dateStyle: NSDateFormatterShortStyle
timeStyle: NSDateFormatterNoStyle];
NSString* timePart = [NSDateFormatter localizedStringFromDate: date
dateStyle: NSDateFormatterNoStyle
timeStyle: NSDateFormatterShortStyle];
NSLog(#"Month Day: %#", datePart);
NSLog(#"Hours Min: %#", timePart);
Well, I believe the following code works for what I need:
NSString *dateComponents = #"yMMd";
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:[NSLocale currentLocale]];
NSArray *tmpSubstrings = [dateFormat componentsSeparatedByString:#"y"];
NSString *tmpStr;
NSRange r;
if ([[tmpSubstrings objectAtIndex:0] length] == 0) {
r.location = 1;
r.length = [[tmpSubstrings objectAtIndex:1] length] - 1;
tmpStr = [[tmpSubstrings objectAtIndex:1] substringWithRange:r];
} else {
r.location = 0;
r.length = [[tmpSubstrings objectAtIndex:0] length] - 1;
tmpStr = [[tmpSubstrings objectAtIndex:0] substringWithRange:r];
}
NSString *newStr = [[NSString alloc] initWithFormat:#"%# H:mm", tmpStr];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:newStr];
NSString *formattedDateString = [formatter stringFromDate:[NSDate date]];

iPhone - Conversion between NSDate - NSString - NSDate

I'm using following code to get NSDate, convert it to NSString and then back t NSDate.
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:#"MM/dd/YYYY"];
NSDate* dt = [NSDate date];
NSString* strDate = [dtFormatter stringFromDate:dt];
NSLog(#"First = %#",strDate);
NSDate* dt2 = [dtFormatter dateFromString:strDate];
NSString* strDate2 = [dtFormatter stringFromDate:dt2];
NSLog(#"Second = %#",strDate2);
But the output I'm getting is different:
2011-02-12 08:17:21.851 SF Calculation[16297:207] First = 02/12/2011
2011-02-12 08:17:21.852 SF Calculation[16297:207] Second = 12/26/2010
Why is it different when converting NSDate to NSString and back from NSString to NSDate?
Instead of :
[dtFormatter setDateFormat:#"MM/dd/YYYY"];
use:
[dtFormatter setDateFormat:#"MM/dd/yyyy"];