NSDate和NSString的转换

用于uidate,picker。。
//将string转换成data类型
    NSData *dataXML = [m_strCurParser dataUsingEncoding:NSASCIIStringEncoding];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:dataXML];


+(NSDate*) convertDateFromString:(NSString*)uiDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyy年MM月dd日"];
    NSDate *date=[formatter dateFromString:uiDate];
    return date;
}



输入的日期字符串形如:@"1992-05-21 13:08:08"

- (NSDate *)dateFromString:(NSString *)dateString{

NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];




NSDate *destDate= [dateFormatter dateFromString:dateString];





[dateFormatter release];

return destDate;

}



- (NSString *)stringFromDate:(NSDate *)date{

NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];



//zzz表示时区,zzz可以删除,这样返回的日期字符将不包含时区信息。

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];



NSString *destDateString = [dateFormatter stringFromDate:date];





[dateFormatter release];

return destDateString;

}



- (IBAction)handleDidPressBtn1{

NSLog(@"%@/n", [selfdateFromString:@"2011-08-09 12:02:01"]);

NSLog(@"%@/n", [selfstringFromDate:[selfdateFromString:@"2011-08-09 12:02:01"]]);

}





输出结果如下:



2011-08-09 04:02:01 +0000

2011-08-09 12:02:01 GMT+08:00

你可能感兴趣的:(NSString)