开发者

objective c and NSDate

开发者 https://www.devze.com 2023-01-03 01:00 出处:网络
I am looking for component that can开发者_运维问答 handle a spesific date. what i am trying to do is to get Astring fron sever that represent date(for example 04-08-2012) in my iphone i want to be ab

I am looking for component that can开发者_运维问答 handle a spesific date.

what i am trying to do is to get Astring fron sever that represent date(for example 04-08-2012) in my iphone i want to be able to "work" with this date. such to compare it to another date , check if the date in the past or future and to print it to the app GUI

i tried work with NSDate but i didnt found how can i set a spesific date?

thanks


You can use an NSDateFormatter

Here is a sample code to parse a date from string:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"%A, %B %d, %Y"];
NSDate* date = [formatter dateFromString:aString];
[date compare:anotherDate];

More about Date Formatter here

The date format string is composed of various elements that pull out portions of the date. %A is the full name of the day of the week, %B is the full name of the month, etc.


You need 3 classes to set the date by representation:

NSCalendar* cal = [NSCalendar currentCalendar]; // 1.
NSDateComponents *comps = [[NSDateComponents alloc] init]; // 2.
[comps setYear:2012];
[comps setMonth:4];
[comps setDay:8];
NSDate* date = [cal dateFromComponents:comps];  // 3.
[comps release];
0

精彩评论

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