在iOS中按“按日期排序”查询工作所需的日期格式(Date format needed to work 'order by date' query in iOS)

编程入门 行业动态 更新时间:2024-10-23 05:34:43
在iOS中按“按日期排序”查询工作所需的日期格式(Date format needed to work 'order by date' query in iOS)

我正在为我的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.

更多推荐

本文发布于:2023-07-31 01:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:所需   日期   格式   按日   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!