0
我只是想创建一个NSDate
对象与字符串的NSDate:2011-02-10 4:30:45
创建格式化使用NSDateFormatter或mktime
看看代码:
方法OBJC:
-(NSDate *)get_date_df:(NSString *)dstr
{
NSLog(@"1> %@",dstr);
NSDateFormatter *df_in = [[[NSDateFormatter alloc] init]autorelease];
[df_in setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *d = [df_in dateFromString:dstr];
NSLog(@"2> %@",d);
NSLog(@"3> %@",[df_in stringFromDate:d]);
return d;
}
这里的输出:
1> 2011-02-10 4:30:45
2> 2011-02-09 23:00:45 +0000
3> 2011-02-10 04:30:45
那么为什么2>
打印错误?
假设在2011-02-10 4:20:45
,我尝试在该日期设置LocalNotification,它会立即触发,因为处理器假定时间已经过期!
UILocalNotification *lnf = [[UILocalNotification alloc] init];
[lnf setFireDate:[self get_date_df:@"2011-02-10 4:30:45"]];
[lnf setAlertBody:@"WTF"];
[[UIApplication sharedApplication]scheduleLocalNotification:lnf];
[lnf release];
而且,我已经尝试了en_US, en_FR
建立各种TimeZones
和NSLocale
。没有帮助。
方法C:
,仅供参考,现在我切换到C,想用的东西:
-(NSDate *)get_date_c:(const char *)date_str
{
char date[10][6] = {0};
sscanf(date_str,"%[0-9] - %[0-9] - %[0-9] %[0-9] : %[0-9] : %[0-9]",date[0],date[1],date[2],date[3],date[4],date[5]);
struct tm *t_info = (struct tm *)malloc(sizeof(struct tm));
t_info->tm_sec = atoi(date[5]);
t_info->tm_min = atoi(date[4]);
t_info->tm_hour = atoi(date[3]);
t_info->tm_mday = atoi(date[2]);
t_info->tm_mon = atoi(date[1]);
t_info->tm_year = atoi(date[0]);
NSLog(@"ans: %f",(double)mktime(t_info));
NSDate *d = [NSDate dateWithTimeIntervalSince1970:mktime(t_info)];
free(t_info);
return d;
}
输出: ans: -1.000000
有我再次坚持了转换time_t
到NSTimeInterval
。
所以,在此先感谢,如果你能帮助我的任何方法:(
但是,在这种情况下,日志#3应该也已经印在UTC – chunkyguy 2011-02-11 02:39:20