English 中文(简体)
将NSString改为NSDate [复制]
原标题:convert NSString to NSDate [duplicate]
  • 时间:2011-09-14 09:27:46
  •  标签:
  • iphone
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
NSString to NSDate

Hi I want to convert the string "OCT 1, 2011 18:30" into NSDate format 2011-05-13 19:00:00 +0800. I have tried with the many formatters but what I am getting is a null value each time. Can any body help me on this?

最佳回答

你可以尝试这一法典,因为我成功运作,这将有助于你。

    NSString *dateStr = @"OCT 1, 2011 18:30";
    NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
    [dtF setDateFormat:@"LLL d, yyyy HH:mm"];
    NSDate *d = [dtF dateFromString:dateStr];
    NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
    [dateFormatStr setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
    NSDate *st = (NSDate *)[dateFormatStr stringFromDate:d];   
    NSLog(@"%@",st);
问题回答

try this

NSDateFormatter *datefor = [[[NSDateFormatter alloc] init] autorelease];
     [datefor setDateFormat:@"MMM dd, yyyy HH:mm"];
     NSDate *date = [datefor dateFromString:@"OCT 1, 2011 18:30"];
    [datefor setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; 
     NSString *str=  [[NSString alloc] initWithString:[datefor stringFromDate:date]];
     NSDate *date_fianl = [datefor dateFromString:str];
      [str release];
     NSLog(@"%@",date_fianl);//it will print 2011-10-01 01:00:00 +0000




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签