-1
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd";
NSTimeZone *gmt =  [NSTimeZone defaultTimeZone];
[dateFormatter setTimeZone:gmt];
NSString *cur_date= [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@",cur_date);

现在我将手动时间设置为今天的日期:

NSString *cur_time=@"13:05:08";

dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date_obj=[dateFormatter dateFromString:[cur_date stringByAppendingFormat:@" %@",cur_time]];
NSLog(@"%@",date_obj);

但我今天要约会NSlog显示为

2012-09-28 07:35:08 +0000

即使我将时间设置为13:05:08.

如何将手动时间设置为当前日期并从中制作一个datetime对象?

4

2 回答 2

0
//Try using Time Interval

[myDate addTimeInterval:entertimeinterval];
于 2012-09-28T09:56:23.277 回答
0

做这个

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd";
//You can either use UTC or GMT
NSTimeZone *gmt =  [NSTimeZone timeZoneWithName:@"GMT"];// This is where I made the change
[dateFormatter setTimeZone:gmt];
NSString *cur_date= [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@",cur_date);    

NSString *cur_time=@"13:05:08";

dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date_obj=[dateFormatter dateFromString:[cur_date stringByAppendingFormat:@" %@",cur_time]];
NSLog(@"date %@",date_obj);

希望这会奏效

于 2012-09-28T09:52:21.167 回答