在特定的小时和分钟为今天创建NSDate [英] Create NSDate for today at a particular hour and minute

查看:109
本文介绍了在特定的小时和分钟为今天创建NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用今天的日期和分别为5,0和0的小时,分​​钟和秒创建 NSDate ?也就是说日期将为07/02/2010 05:00:00

How can I create an NSDate with today's date and an hour, minutes, and seconds of 5, 0, and 0 respectively? I.e. the date will be 07/02/2010 05:00:00

推荐答案

//Gives us the current date
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit fromDate:[NSDate date]];
[components setHour:5];
NSDate * date = [gregorian dateFromComponents:components];
[gregorian release];

沿着这些线的东西应该做的。

Something along those lines should do the trick.

这篇关于在特定的小时和分钟为今天创建NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆