devaditya
devaditya

Reputation: 331

Saving Timestamp into sqlite

18.11.2009 10:32:00

I want the value in between the above tag(created) to be inserted into the sqlite db for which i have taken a column of type timestamp.... how can i store this value in that column?? please advise...

Upvotes: 0

Views: 3613

Answers (4)

Sudhanshu
Sudhanshu

Reputation: 3960

Well, it depends in which format you want to save it in your database.

If you want to save it in the string form, then save it directly by making an object. But, use the datatype of string type.

Another option is to save it using the date datatype, seeing as sqlite doesn't have a dedicated date/time datatype.

Use a formatter to set the date format:

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MM-YYYY HH:MM:SS"];

Then make Date object, and save it.

Upvotes: 2

Ishu
Ishu

Reputation: 12787

Make object for date

then use this

[date timeIntervalSinceDate:objDate];

this give time interval beetween the date and objDate. two dates(date and objDate) for finding timeInterval.

Save this by convert it into string.

Upvotes: 0

amphetamachine
amphetamachine

Reputation: 30651

Two options here (if you want sorting, which I assume you do):

  • Add as a string, but re-order the fields from highest to lowest. After that, it's a simple matter to sort via text. This is the slower option.

2009.11.18 10:32:00

  • Convert to a UNIX timestamp, I.E. seconds since Jan 1st, 1970. This is the fastest option.

1261153920

It is then simple to pull it out using date and time functions.

Note that SQLite does not have a date/time data type.

Upvotes: 0

maid450
maid450

Reputation: 7486

You should replace dots by hyphens and place months, days and year parts in correct order. According to sqlite docs these are the date and time accepted formats:

  1. YYYY-MM-DD
  2. YYYY-MM-DD HH:MM
  3. YYYY-MM-DD HH:MM:SS
  4. YYYY-MM-DD HH:MM:SS.SSS
  5. YYYY-MM-DDTHH:MM
  6. YYYY-MM-DDTHH:MM:SS
  7. YYYY-MM-DDTHH:MM:SS.SSS
  8. HH:MM
  9. HH:MM:SS
  10. HH:MM:SS.SSS
  11. now
  12. DDDDDDDDDD

Upvotes: 1

Related Questions