从iPad中的数据库中读取日期
举例来说,我有以下代码段,我用它从数据库中检索日期
DateLabel.text = [CommonHelper getDateString:objSample.startDate :@"MM/dd/yyyy"];
,其中
i) objSample
是我的 Sample
实体的对象
ii) startDate
是 Sample
实体中的一个属性,声明为 @property (nonatomic, keep) NSDate * startDate;
iii) CommonHelper
是另一个属性文件,其中我有 getDateString:
函数,如下所示,
+ (NSString *) getDateString: (NSDate *) date: (NSString *) format
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:format];
NSString *dateStr = [dateFormat stringFromDate:date];
[dateFormat release];
return dateStr;
}
使用 Sqlite 数据库浏览器,如果我在示例的 startDate 属性中输入值“04/23/1999”,则该值将显示在 DateLabel.text
是一个完全不同的值,如果我输入“76851234”等值,则 DateLabel.text
中显示的值是“05/23/2001”。
由于我是处理数据库的新手,我无法理解为什么会发生这种情况...... 有人能告诉我为什么会发生这种情况吗?有没有办法可以使用 SQLite 数据库浏览器直接将日期输入数据库以便显示该日期?
我输入 SQLite 数据库浏览器的日期格式是“Unix 日期格式”吗?如果是这样,我如何才能将日期转换为unix日期格式,以便我确定我将在我的应用程序上显示的确切日期......
say for example I have the following piece of code with which I retrieve the date from the database
DateLabel.text = [CommonHelper getDateString:objSample.startDate :@"MM/dd/yyyy"];
where
i) objSample
is an object of my Sample
entity
ii) startDate
is an attribute in the Sample
entity declared as @property (nonatomic, retain) NSDate * startDate;
iii)CommonHelper
is another file where I have the getDateString:
function as below,
+ (NSString *) getDateString: (NSDate *) date: (NSString *) format
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:format];
NSString *dateStr = [dateFormat stringFromDate:date];
[dateFormat release];
return dateStr;
}
Using Sqlite Database Browser, if I enter the value as "04/23/1999" to the Sample's startDate attribute , the value which is displayed in DateLabel.text
is a totally different value and if I enter a value such as "76851234", the value which is displayed in DateLabel.text
is "05/23/2001".
Since I am new in dealing with database, I am unable to understand why this happens...
Would someone be able to tell me why this happens? and is there a way I can directly enter the date into the database using SQLite Database Browser so that that date is displayed?
is the format of the date I enter into the SQLite database browser a "Unix Date Format"? if so, how would I be able to convert the date into unix date format so that I know for certain what would be the exact date I would be displaying on my app...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是你不能。
SQLite 是“无类型”。这意味着您可以在任何表的任何列中存储所需的任何类型的数据,无论该列声明的数据类型如何。 http://www.sqlite.org/datatypes.html
The answer for this is that you can't.
SQLite is "typeless". This means that you can store any kind of data you want in any column of any table, regardless of the declared datatype of that column. http://www.sqlite.org/datatypes.html