将NSDate转换为另一种格式的NSString和NSdate

时间:2015-07-17 04:58:19

标签: ios nsstring nsdate nsdateformatter

我使用以下方法将NSDate转换为NSString

- (void)splitDateintoDateTime:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"dd/MM/yyyy"];
    strDate = [formatter stringFromDate:date];

    [formatter setDateFormat:@"hh:mm a"];
    strTime = [formatter stringFromDate:date];
}

date的格式为yyyy-MM-dd hh:mm:ss z

我希望DateTime的单独NSDate值采用方法中提到的格式。

1 个答案:

答案 0 :(得分:-1)

- (void)splitDateintoDateTime:(NSDate *)date
{

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
   dateFormatter.dateFormat = @"dd/MM/yyyy";
   NSString *dateString = [dateFormatter stringFromDate: date];
   NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
   timeFormatter.dateFormat = @"HH:mm z";
   NSString *dateString = [timeFormatter stringFromDate: date];
   }