2013-09-28 112 views
-3
{  
    NSDateFormatter *dateReader = [[NSDateFormatter alloc] init]; 

    [dateReader setDateFormat:@"dd-MM-YYYY"]; // for example 

    NSDate *itsDate = [dateReader dateFromString:txtDate.text]; 
} 

我使用此代碼來從字符串轉換日期,我從UITextField文本中獲取字符串。不能從字符串轉換日期

但是每次我得到「2013-01-04 18:30:00 +0000」這個日期連我的文本字段的日期都不一樣。 所以我不能得到正確的日期。 我該怎麼辦?

+0

用戶類型date in textfield?如果是的話,那麼可能這不是一個好主意,你應該向用戶提供一個日期選擇器來選擇所需的日期。 –

+0

閱讀罰款[手動](http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns)。這是「yyyy」,而不是「YYYY」。 –

+1

用戶沒有輸入日期,顯而易見的是,用戶從日期選擇器中選擇日期,因爲該文本框的輸入視圖是日期選擇器 –

回答

1

您可以使用「datefromstring」烏拉圭回合問題,實現自己的目標更改日期格式。在這裏我舉了一個例子:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd MMM yyyy "]; 
NSDate *date1 = [dateFormatter dateFromString:YourDate_String]; 
NSLog(@"Date: %@",date1); 
[dateFormatter release]; 
0

這些都是一些函數,一定會幫助你根據你的輸入和輸出

-(NSDate *)convertStringToDate:(NSString *)stringDate 
{ 
    stringDate=[self trimString:stringDate]; 
    stringDate = [stringDate stringByReplacingOccurrencesOfString:@"+00:00" withString:@""]; 
    stringDate = [stringDate stringByReplacingOccurrencesOfString:@"T" withString:@" "]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
    [dateFormatter setDateFormat:@"dd-MM-yyyy"]; 
    NSDate *myDate = [dateFormatter dateFromString:stringDate]; 
    return myDate; 
} 

-(NSDate *)convertDateToDate:(NSDate *) date 
{ 
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
NSDate *nowDate = [[[NSDate alloc] init] autorelease]; 
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
[formatter setDateFormat:@"yyyy-MM-d H:m:s"]; 
NSString * strdate = [formatter stringFromDate:date]; 
nowDate = [formatter dateFromString:strdate]; 
return nowDate; 
} 

-(NSString *)convertStringToString:(NSString *)DateLocal 
{ 
    DateLocal = [self trimString:DateLocal]; 
    DateLocal=[DateLocal stringByReplacingOccurrencesOfString:@" GMT" withString:@""]; 
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss"]; 
    NSDate *date = [dateFormatter dateFromString: DateLocal]; 
    NSString *tt = [dateFormatter stringFromDate:date]; 
    NSDate *dateReturn = [dateFormatter dateFromString:tt]; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"EEE. d MMMM yyyy, hh:mma"];//"Fri. 1 June 2012, 10:29AM" 
    NSString *dateString = [dateFormat stringFromDate:dateReturn]; 
    [dateFormat release]; 
    return dateString; 
} 

-(NSString *)convertDatetoString:(NSDate *)date 
{ 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    NSString *dateString = [dateFormat stringFromDate:date]; 
    [dateFormat release]; 
    return dateString; 
} 

-(NSString *)dateFormatingCommonDateToString :(NSDate *)date formateString:(NSString *)strDateFormat 
{ 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:strDateFormat]; 
    NSString *dateString = [dateFormat stringFromDate:date]; 
    [dateFormat release]; 
    return dateString; 
} 

-(NSDate *)dateFormatingCommonStringToDate :(NSString *)date formateString:(NSString *)strDateFormat 
{ 
    date=[self trimString:date]; 
    date = [date stringByReplacingOccurrencesOfString:@"+00:00" withString:@""]; 
    date = [date stringByReplacingOccurrencesOfString:@"T" withString:@" "]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
    [dateFormatter setDateFormat:strDateFormat]; 
    NSDate *myDate = [dateFormatter dateFromString:date]; 
    return myDate; 
} 

#pragma mark - 
#pragma mark String Methods 

-(NSString*)trimString:(NSString *)theString 
{ 
    NSString *theStringTrimmed = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
return theStringTrimmed; 
} 
相關問題