Recover a date with SQLite - sql

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];

Related

Detaching a string into two using objective c [duplicate]

This question already has answers here:
Separate 1 NSString into two NSStrings by whiteSpace
(2 answers)
Closed 10 years ago.
I am getting the date from data base in this format, '01/02/2013 17:00'. I have to detach the date and time and put it in separate columns in the nstableview (date in date column and time in time column). How should i do this?
You can:
NSArray *dateAndTime = [#"01/02/2013 17:00" componentsSeparatedByString:#" "];
This if you are certain about that format. Otherwise you should be careful and rely on something like NSDateFormatter.
NSString *dateTime = #"01/02/2013 17:00";
NSArray *strings = [dateTime componentsSeparatedByString:#" "];
The array strings will now look like...
[
#"01/02/2013",
#"17:00"
]
If you want to be able to change to time format, or want more control over the times etc. I recommend a NSDateFormatter..
NSString *dateString = #"'01/02/2013 17:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/YYYY HH:mm"];
NSDate *date = [formatter dateFromString:dateString];
[formatter setDateFormat:#"dd/MM/YYYY"];
NSString *newDateString = [formatter stringFromDate:date];
[formatter setDateFormat:#"HH:mm"];
NSString *newTimeString = [formatter stringFromDate:date];

Creating NSDate from NSString.

I have stored a date in a text file, and I can read it out properly. The output for this string is
"6:00 PM"
Just like that. However, when I do:
//First, set the first datePicker
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSString *breakfastTimeStartLocation = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"breakfastTimeStart.txt"];
NSString *time = [NSString stringWithContentsOfFile:breakfastTimeStartLocation encoding:NSUTF16StringEncoding error:nil];
NSLog(#"Loaded time is %#", time);
NSDate *startDate = [dateFormatter dateFromString:time];
NSLog(#"Start Date = %#", startDate);
In this, time returns the "6:00 PM", but startDate returns "(null)"
I must be missing something, why in the world does this not work?
Thanks.
I believe the issue is that you never set a Date Format. Even if this is not the only problem it would probably be one. Try something like:
[dateFormatter setDateFormat:#"MM/dd/YYYY"];
That's not the right way to init an NSDateFormatter, the designated initializer is initWithDateFormat:allowNaturalLanguage:
Thus:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]
initWithDateFormat:#"h:mm a" allowNaturalLanguage:NO];

date from string not working

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.

dateFromString returns the wrong date

I have the following code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-mm-dd";
NSDate *cDate = [formatter dateFromString:thisLine];
NSLog(#"cDate '%#' thisLine '%#'", cDate, thisLine);
NSLog prints:
cDate '2011-01-10 05:07:00 +0000' thisLine '2011-07-10'
while cDate should be '2011-07-10'
Any thoughts?
Thanks
Lowercase mm is for minutes not months, month use uppercase MM:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd";
NSDate *cDate = [formatter dateFromString:thisLine];
NSLog(#"cDate '%#' thisLine '%#'", cDate, thisLine);
The NSDate description will always print the date with its own formatting, generally for the +000 time zone. You need to use the date format to get the correctly formatted date and use MM for month not mm.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd";
NSDate *cDate = [formatter dateFromString:thisLine];
NSLog(#"cDate '%#' thisLine '%#'", [formatter stringFromDate:cDate], thisLine);
-(NSString*)description
Discussion The representation is not guaranteed to remain
constant across different releases of the operating system. To format
a date, you should use a date formatter object instead (see
NSDateFormatter and Data Formatting Guide)

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]];