开发者

How to get full timestamp with only passing hours and minutes with NSDateFormatter?

I think this is just a minor problem. After searching for some time I still can't figure it out. Hopefully you can. :-)

I want to get a full timestamp of today, passing own hours and minutes. My approach was the following(dateStr is @"11:30" for exampl开发者_开发知识库e):

NSDateFormatter *myFormatter = [[[NSDateFormatter alloc] init] autorelease];
[myFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[myFormatter setDateFormat:@"HH:mm"];
NSDate *tmpDate = [myFormatter dateFromString:dateStr];
[myFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
...???

So now I get

1970-01-01 11:30:00 +0000

What do I habe to do to apply the dateStyle to my NSDate and get the correct date of today? I know that I can use [NSDate date] to get the actual date but what can I do to "manipulate" only minutes and hours?

Would be great to get some help from you guys :)


NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
[components setHour:hour];
[components setMinute:minute];
NSDate *date = [gregorian dateFromComponents:components];

[gregorian release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜