我正在为我的iOS应用程序存储sqlite中的数据列表。 在DB中,一列'addedOn' 以格式'30 -Jul-2014 07:43:20'存储服务器响应的日期。 在这里,我在'addedOn'列上尝试了很多东西但是无法在ASC / DESC顺序中获取日期。 其他一些细节是
列'addedOn'日期类型为DATETIME。 尝试在DESC中使用的查询是:"SELECT * FROM tableName ORDER BY datetime(addedOn) DESC LIMIT 1" & "SELECT * FROM tableName ORDER BY date(addedOn) DESC LIMIT 1" & "SELECT * FROM tableName ORDER BY addedOn DESC LIMIT 1".
但所有这些查询都没能给我预期的结果。
我试图以另一种格式转换此日期,同时在数据库中插入它自己,但转换其他日期格式的日期格式,如'yyyy-MM-dd HH:mm:ss'根本不起作用。 对于尝试下面的代码:
NSString * dateString = @“30-Jul-2014 07:43:20”; NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@“yyyy-MM-dd HH:mm:ss”]; NSDate * dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormatter dateFromString:dateString];
NSString *stringDate = [dateFormatter stringFromDate:dateFromString]; NSLog(@"stringDate %@", stringDate);所以我的问题是,这可以转换为任何其他格式的这种类型格式,或者是否有任何我想念的东西。 请分享您的想法并建议我需要做的更改。 任何帮助表示赞赏。
I am storing list of data in sqlite for my iOS app. In DB one column 'addedOn' stores dates from server response in format '30-Jul-2014 07:43:20'. Here I have tried many things on 'addedOn' column but could not fetch date in ASC/DESC order. Some other details are
Column 'addedOn' date type is DATETIME. Queries tried to fetch in DESC used are:"SELECT * FROM tableName ORDER BY datetime(addedOn) DESC LIMIT 1" & "SELECT * FROM tableName ORDER BY date(addedOn) DESC LIMIT 1" & "SELECT * FROM tableName ORDER BY addedOn DESC LIMIT 1".
But all these queries failed to give me expected result.
I tried to convert this date in another format while inserting in DB it self but converting of this date format in other date format like 'yyyy-MM-dd HH:mm:ss' isn't working at all. For that tried below code:
NSString *dateString = @"30-Jul-2014 07:43:20"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormatter dateFromString:dateString];
NSString *stringDate = [dateFormatter stringFromDate:dateFromString]; NSLog(@"stringDate %@", stringDate);So my question is this possible to convert this type format in any other format or is there any thing else that I am missing. Please share your thought and suggest me the changes that I need to do. Any help is appreciated.
最满意答案
您的dataFormat似乎是错误的。 尝试以下DateFormat。
[dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm:ss"]; NSDate *date = [dateFormatter dateFromString:yourDateString];这将为您提供确切的日期。
希望能帮助到你..
Thanks folks for your time and suggesting me changes that I need to do. Storing of NSDate as it is in DB instead of Converting NSDate in NSString and then inserting in DB solved my problem with little change in code
There are two cases in which my code works now
NSString *dateString = MD_history.addedOn; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"dd-MMM-yyyy HH:mm:ss"]; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [df dateFromString:dateString];And
NSString *dateString = MD_history.addedOn; NSDateFormatter *df = [[NSDateFormatter alloc] init]; NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; [df setLocale:usLocale]; [df setDateFormat:@"dd-MMM-yyyy HH:mm:ss"]; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [df dateFromString:dateString];In this case Date after inserting in DB is in "2014-07-30 02:13:20 +0000" format. In this sorting of date column works proper.
更多推荐
发布评论