Andiana
Andiana

Reputation: 1952

How to save Date type data as string with format "dd-MM-yyyy" in SQLite?

The Sqlite normal date string format is yyyy-MM-dd. How can I save it as dd/MM/yyyy? Should I keep the yyyy-MM-dd format when working with the database and use dd/MM/yyyy only with the end user interface? Edit 1: I want to save my date datas as 'dd/MM/yyyy' in SQLite to be the same as my locale date format. But the date comparison is return wrong result with this format. I changed the date string in DB to 'yyyy-MM-dd' and the date comparison work perfect. Is 'yyyy-MM-dd' is the only string format which SQLite can understand as date?

Upvotes: 0

Views: 6638

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520968

You can use the strftime() function which accepts as its first parameter a format string:

SELECT strftime('%d/%m/%Y', 'now'); 

Output:

06/10/2015

The strftime() function gives the ability to convert Y-m-d into d/m/Y or any other format you want. So you can continue to store your date data as is in the database, and you can query it out however you wish.

Upvotes: 2

princ___y
princ___y

Reputation: 1097

you can use date formatter code:

NSString *myString = @"2012-11-22 10:19:04";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; 
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = @"dd-MMM-yyyy";
NSLog(@"%@",[dateFormatter stringFromDate:yourDate]);

Upvotes: -1

Related Questions