开发者

NSDateFormatter: How to Shorten Formatted Date?

开发者 https://www.devze.com 2023-04-10 17:13 出处:网络
I\'m using an NSDateFormatter to format my date in SQLite table cell value. The type of my cell is datetime, and the values I enter are like: \"01.11.2011\", \"02.11.2012\" (dd.MM.yyyy)

I'm using an NSDateFormatter to format my date in SQLite table cell value.

The type of my cell is datetime, and the values I enter are like: "01.11.2011", "02.11.2012" (dd.MM.yyyy)

When I'm getting the data, I'm using an NSDateFormatter like this:

NSDateFormatter *dateFormatter =  [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@开发者_JS百科"dd.MM.yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

And my output values are like this:

2011-11-01 00:00:00 +0000

How can I shorten this value as a date value only? Like 2011-11-01?

And more, how can I change the places and separators in this value like 01.11.2011?

Edit: My mistake, I didn't show how I got the values. Like this:

NSDate *myDate = [dateFormatter dateFromString:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)]];

Edit-2: I put a breakpoint and checked the coming value from this string, it's like 01.11.2011, as I entered the database.


Your code is pretty much correct. You didn't show how you get the output from the dateFormatter though. With the formatter format set to:

[formatter setDateFormat:@"dd.mm.yyyy"];
NSString *formattedDate = [formatter stringFromDate:[NSDate date]];

This should assign formattedDate with

04.10.2011
0

精彩评论

暂无评论...
验证码 换一张
取 消