{
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 :(得分:1)
你可以使用" datefromstring#34;为你的问题。这里有一个例子:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy "];
NSDate *date1 = [dateFormatter dateFromString:YourDate_String];
NSLog(@"Date: %@",date1);
[dateFormatter release];
答案 1 :(得分: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;
}