HELLO i am trying to insert Current date in my database table
i have a column dateVisited of type datetime
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy/MM/dd HH:mm:ss"]; //this is the sqlite's format
NSDate *stringTime = [NSDate date];
NSString *formattedDateStringTime = [formatter stringFromDate:stringTime];
sqlStr = [NSString stringWithFormat:#"Insert into AnwaltHistory (id,dateVisited) values (%d,'%#')",Id formattedDateStringTime];
NSLog(#"%#",sqlStr);
BOOL Res = [appDelegate InsUpdateDelData: sqlStr];
if (Res)
{
NSLog(#"The Following instrunction is excuted successfully !");
}
else
{
NSLog(#"The Following instrunction Failed to execute !");
}
the statement executes successfully but when i actually go and check the table it says invalid date... i dont know why
as i have printed sqlstr i copy paste from the console and paste it in sqlite and run it manually it runs perfectly fine and has the actual date....
can any one guide me where i am mistaken :S.......
please
Why not use:
"Insert into AnwaltHistory (id,dateVisited) values (%d,datetime())",Id
You could try this:
double dateVisited = [[NSDate date] timeIntervalSince1970]; //now
// save dateVisited to your 'dateVisited' field
According to the official documents, the DateTime on SQLITE should be in ISO8601 format (AKA: a string of ("YYYY-MM-DD HH:MM:SS.SSS"))
Therefore, to store the date, you should format your date string, below is a code to help you with this:
NSDate *SourceDate = [NSDate date]; // Or whatever NSDATE you like...
NSDateFormatter *MyDateFormatter = [[NSDateFormatter alloc] init];
[MyDateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSString *SQLiteQuery = [NSString stringWithFormat:#"Insert into YourTable(YourDateField) values ('%#')", [MyDateFormatter stringFromDate:SourceDate]];
This should do it, give's a shout if you need more help,
Cheers
H
Related
NSString *nameToSave = word;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *fromDateString=[dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
nameToSave = [word stringByAppendingString:fromDateString];
nameToSave = [nameToSave stringByAppendingString:#".txt"];
NSLog(#"Server notes name .............>>>>>>!!!! %#",nameToSave);
for saving filenames i am using dateformatter, my problem is in the above code word is same for two files and it is run within one second then two file names are same. So the files are overriding.., i think with use of milliseconds i can solve my problem, But i don't know how to get current time in milliseconds. [ seconds/1000 or any arithmetic operations is not helpful for me..., because after applying arithmetic operation also we get same name]
You can set a date format like
[dateFormatter setDateFormat:#"yyyy-MM-dd-HH:mm:ss:SSS"];
"SSS" is for milliseconds.
just do like this [[NSDate date] timeIntervalSince1970];
I am getting the rfc3399 formatted date string through google calender web services. I want to convert this string into NSDate object so that I can add an event about this to the local calendar. I found the below method in Apple's documentation regarding NSDateFormatter , but its not working
Date String - 2012-05-23T18:30:00.000-05:00
- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString {
/*
Returns a user-visible date time string that corresponds to the specified
RFC 3339 date time string. Note that this does not handle all possible
RFC 3339 date time strings, just one of the most common styles.
*/
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
NSString *userVisibleDateTimeString;
if (date != nil) {
// Convert the date object to a user-visible date string.
NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init];
assert(userVisibleDateFormatter != nil);
[userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
[userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];
userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}
return userVisibleDateTimeString;
}
Please help me , I have tried changing the date format quite a lot times but no success.
try with this method..
-(NSDate *)convertStringToDate:(NSString *) date {
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:#"YYYY-MM-DD'T'HH:mm:ssZ"];// set format here which format in string date
/// also try this format #"yyyy-MM-dd'T'HH:mm:ss.fffK"
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
call this method like bellow..
NSDate *date = [self convertStringToDate:rfc3339DateTimeString];
also for try with some rfc Date formate with fffK for this format see this bellow. link..
how-do-i-parse-and-convert-datetimes-to-the-rfc-3339-date-time-format
i hope this answer is helpful
The issue is the date contains (-05:00 )':' in the timezone value ... you have to remove it from the string then the above method will work fine......The new date string should look like below
2012-05-23T18:30:00.000-0500 - This should be the right format .
im using a string to convert it to an nsDate
is working fine,
but when i try to add time to my string I get null on the logs
NSString *dateStringus = [NSString stringWithFormat:#"12-09-2011"];
NSLog(#"%#",dateStringus);
NSDateFormatter *dateFormattero = [[NSDateFormatter alloc] init];
[dateFormattero setDateFormat:#"dd-MM-yyyy"];
NSDate* d = [dateFormattero dateFromString:dateStringus];
NSLog(#"tomad tu string date ::%#",d);
NSString *pingus = [dateFormattero stringFromDate:d];
NSLog(#"transumatada ::%#", pingus);
NSLog(#"\n");
Works fine for only date,
but
if I do
NSString *dateStringus = [NSString stringWithFormat:#"12-09-2011 14:14:00"];
I get null on the logs
how to fix this string to include time? thanks!
The dateFormat property of the NSDateFormatter dateFormattero must match the format of date/time in the string dateStringus.
Change:
[dateFormattero setDateFormat:#"dd-MM-yyyy"];
Into:
[dateFormattero setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
Here is a good reference on date format patterns.
I am having a date format "16-11-2011". but I want to convert it into "16 Nov"
I written the code for it but null value generates instead of the format "16 Nov"
for this I have written following code
NSDate *checkIn = [[NSUserDefaults standardUserDefaults]valueForKey:#"CHECK_1"];
NSLog(#"Date: %#",checkIn);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMM"];
NSString *checkInDate = [formatter stringFromDate:checkIn];
NSLog(#"checkInDate:%#",checkInDate);
and the results display by nslog statements are as follows:
Date: 16-11-2011
checkInDate:(null)
so I am not able to convert it into the format which I want
plz help me to solve the problem.
I suspect that checkIn is a NSString not NSDate.
Therefore, you would have to convert it to a NSDate first and then convert it back to the desired string format.
NSString *checkIn = [[NSUserDefaults standardUserDefaults]valueForKey:#"CHECK_1"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *checkInTempDate = [dateFormatter dateFromString:checkIn];
[dateFormatter setDateFormat:#"dd MMM"];
NSString *checkInDate = [dateFormatter stringFromDate:checkInTempDate];
The problem is in the first line:
NSDate *checkIn = [[NSUserDefaults standardUserDefaults]valueForKey:#"CHECK_1"];
NSUserDefaults does not have a method valueForKey, it is Key Value Coding" that is returning a value. NSUserDefaults also does not handle dates. What you are getting back from NSUserDefaults is a string, by a quirk NSDate is accepting that.
The last 4 lines of code are correct but need a date, checkIn is a string.
To test this put this line of code after the first line, ignore any incompatible type warning):
NSLog(#"class name: %s", class_getName([checkIn class]));
my question is simple, i want to load a UIImage from an URL, but this URL change programmatically by date.
Example Today the url is http://www.abc.com/2011-10-13/alfa.jpg
tomorrow is http://www.abc.com/2011-10-14/alfa.jpg
the only thing that change is the date part, how can i figure my app load that "alfa.jpg" at current date everytime i start it?
Thanks!
You should use [NSDate date] which returns the current date and time according to the device time (assuming your device time doesn't differ from the actual by more than a day). Then you should format the date according to your url. Something like-
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString* dateString = [formatter stringFromDate:[NSDate date]];
[formatter release];
Use an NSDateFormatter to generate the part of the string that varies with time.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString * datePart = [dateFormatter stringFromDate:[NSDate date]];
NSString * theURLString = [NSString stringWithFormat:#"http://www.abc.com/%#/alfa.jpg", datePart];
You will have to maintain a mechanism to check whether if the image for the day has been downloaded to avoid downloading it again.
Create a template of the url you are using nstring and a DateFormatter object...
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"yyyy-MM-dd"];
NSString *todayStr = [df stringFromDate:[NSDate date]];
NSString *todayUrl = [NSString stringWithFormat:#"http://www.abc.com/%#/alfa.jpg", todayStr];
todayUrl has the url for today!
Its an easy task just format your URL string using current date like,
NSString *URLString = [[NSString alloc] initWithFormat:#"http://www.abc.com/%#/alfa.jpg", currentDate];
Then use this string for URL request.