iPhone中字符串到日期的转换
我在这里引用了前面的例子。但仍然存在空值问题 字符串值为:==>strDate=“09-05-2012 00:00:00” 我希望输出为:==>2012年5月9日 我的代码:iPhone中字符串到日期的转换,iphone,nsdate,nsdateformatter,Iphone,Nsdate,Nsdateformatter,我在这里引用了前面的例子。但仍然存在空值问题 字符串值为:==>strDate=“09-05-2012 00:00:00” 我希望输出为:==>2012年5月9日 我的代码: NSString *strDateFormat;//[strDate substringWithRange:NSMakeRange(0, 10)]; strDateFormat = [NSString stringWithFormat:@"%@", strDate]; NSDateFormatter *df = [[NS
NSString *strDateFormat;//[strDate substringWithRange:NSMakeRange(0, 10)];
strDateFormat = [NSString stringWithFormat:@"%@", strDate];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSDate *disDate = [df dateFromString:strDateFormat];
[df setDateFormat:@"MMM dd, yyyy"];
strDateFormat = [df stringFromDate:disDate];
lblDate.text = [NSString stringWithFormat:@"%@", strDateFormat];
[df release];
它给出(null)值。因为disDate具有(null)值
为什么disDate设置为零。。?
任何解决方案?您尚未设置格式化程序模式
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:@"MMM dd, yyyy"];
NSDate *convertedDate = [df dateFromString:origDate];
[df release];
您尚未设置格式化程序模式
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:@"MMM dd, yyyy"];
NSDate *convertedDate = [df dateFromString:origDate];
[df release];
这样称呼它
NSString *strDate = [self getStringWithFormat:@"09-05-2012 00:00:00" fromFormat:@"dd-MM-yyyy HH:MM:SS" toFormat:@"MMM dd, yyyy"];
这是一个常用函数,您可以将其用于任何格式,只需在参数中传递该格式即可
这样称呼它
NSString *strDate = [self getStringWithFormat:@"09-05-2012 00:00:00" fromFormat:@"dd-MM-yyyy HH:MM:SS" toFormat:@"MMM dd, yyyy"];
这是一个常用函数,您可以将其用于任何格式,只需在参数中传递该格式。您还必须告诉格式化程序在读取字符串时所需的格式
NSString *string = @"09-05-2012 00:00:00";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:string];
[formatter setDateFormat:@"MMM dd, yyyy"];
NSString *formattedString = [formatter stringFromDate:date];
您还必须告诉格式化程序在读取字符串时所需的格式
NSString *string = @"09-05-2012 00:00:00";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:string];
[formatter setDateFormat:@"MMM dd, yyyy"];
NSString *formattedString = [formatter stringFromDate:date];
嘿,我用过。但仍然返回空值。NSDate*fromFormatDate->使用值。现在有什么想法吗?请传递与字符串Ex相同的fromFormat。如果您的字符串是2011年12月5日,那么fromFormat应该是我使用过的MM/dd/YYYY。但仍然返回空值。NSDate*fromFormatDate->使用值。现在有什么想法吗?请传递与字符串Ex相同的fromFormat。如果字符串为2011年12月5日,则fromFormat应为MM/dd/yyyy