2013-02-10 116 views
-5

我使用[NSDate date]以格式"2013-02-10 09:26:52 +0000"獲取當前日期。
我想要的日期格式如"Feb 10"以指定格式獲取日期

任何人都可以提出正確的dateFormat

+1

這已被問過很多很多次......請搜索「[目標c]日期格式」。 – trojanfoe 2013-02-10 10:00:25

回答

0
NSString *string = @"2013-02-10 09:26:52 +0000"; // The date in a string 

//Convert the string to date with the same format 
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"]; // Set format 
NSDate *date = [dateFormat dateFromString:string]; 

//Create the new date format using NSDateFormatter 
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init]; 
[dateFormat1 setDateFormat:@"MMM dd"]; // Set format 

//Convert the date using dateFormat1 
NSDate *formatDate= [dateFormat1 dateFromString:date]; // date with MMM dd 
+0

謝謝。它工作正常... – Sujit 2013-02-10 12:51:50

0

喜設定日期格式這樣

[dateFormatter setDateFormat:@"MMM d, yyyy hh:mm:ss"]; 

希望它會工作

0

它是工作在我的情況:

  NSDateFormatter* dateFormatterBegin = [[NSDateFormatter alloc] init]; 
      [dateFormatterBegin setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"]; 
      NSString *dateStringBegin = @"2013-02-10 09:26:52 +0000"; 
      NSDate* datebegin = [dateFormatterBegin dateFromString:[dateStringBegin stringByReplacingOccurrencesOfString:@":" withString:@"" options:0 range:NSMakeRange(dateStringBegin.length-3, 1)]]; 
      [dateFormatterBegin setDateFormat:@"MMM-dd"]; 
      NSString *beginDate = [[NSString alloc] initWithFormat:@"%s%@","Start Date : ",[dateFormatterBegin stringFromDate:datebegin]]; 

      NSLog(@"Your date is : %@",beginDate); 

      [dateFormatterBegin release]; 
      [beginDate release];