2016-04-05 149 views
-2

我知道这个问题已被问了这么多时间,可能是一些问题的重复,实际上我试图通过在String中转换它们将日期存储到数组中。我需要在NSDate格式的值,所以我再次将存储的字符串转换为日期。问题与格式日期

NSDateFormatter *dateformat = [[NSDateFormatter alloc] init]; 
[dateformat setDateFormat:@"MM-dd-yy-hh-mm-ss"]; 

NSString *date = [dateformat stringFromDate:datePicker.date]; 
[kAppDelegate.glbArrName addObject:date]; 

但我得到这样的输出:

NSString *date = [kAppDelegate.glbArrDate objectAtIndex:indexPath.row]; 

NSLog(@"Date of birth %@",date); 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 

[dateFormatter setDateFormat:@"MM-dd-yy-hh-mm-ss"]; 
NSDate *birthDate = [[NSDate alloc] init]; 

birthDate = [dateFormatter dateFromString:date]; 

NSLog(@"Date of birth after formatting %@",birthDate); 

输出是:

Date of birth 04-05-16-06-38-14 
Date of birth after formatting 2016-04-05 01:08:14 +0000 

为什么它的变化形式,因为我已经做了与以前相同。请帮助我找出答案。

+0

为什么麻烦转换字符串和从字符串转换?一个数组可以容纳一个'NSDate'而没有任何问题。 – Avi

+0

其实我也需要格式化,如何直接从uipicker.date获得NSdate,也格式化..我实际上得到字符串 –

+0

在你的数组中存储'NSDate'。除非需要将其显示给用户,否则不要将其转换为字符串。 – rmaddy

回答

0

您正在将日期字符串重写回NSDate,然后打印出NSDate,默认为后者2016-04-05 01:08:14 +0000格式,作为NSDate的-description的一部分。

+0

我明白了你的观点,但是如何得到确切的日期 –