开发者

How can I convert string into date?

I have two strings: date1 = 3-5-2014; date2 = 4-2-2010;

I have to convert them in date and then compare them. I want the same format of date as in string开发者_如何学Cs i.e., dd-mm-yyyy. How it can be done?


NSString *string=@"03-05-2014";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:string];

enjoy...


You can extract NSDate objects from strings using the NSDateFormatter class. See the Date Formatters section Apple's Data Formatting Guide for a detailed explanation and sample code.


Try with below

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy h:mm a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];

here is the SO post

Convert NSString date to NSDate


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSDate *date = [dateFormatter dateFromString:@"25-8-2010"];
    NSLog(@"%@", date);
    [dateFormatter release];

Hope this works for you
Cheers :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜