将手动时间设置为今天日期,并在ios6中创建datetime对象

时间:2012-09-28 09:46:42

标签: iphone ios ios-simulator ios6

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对象?

2 个答案:

答案 0 :(得分: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);

希望这会起作用

答案 1 :(得分:0)

//Try using Time Interval

[myDate addTimeInterval:entertimeinterval];