如何以编程方式在 nsdate 中设置小时分钟?

发布于 2024-09-04 09:24:57 字数 140 浏览 2 评论 0原文

我在一个标签中有 2:01:20 PM 格式;经过一些计算,我需要更改该标签的小时和/或分钟。

也就是说,是否可以通过将其转换为 NSDate 对象并修改小时和分钟(如 3:20:30 PM)来更改该标签值?

有没有这方面的设置方法?

I have 2:01:20 PM format in one label; after some calculations, I need to change the hour and/or minute of that label.

That is ,Is it possible to change that label value, by converting it as NSDate object and modifying hour and minute like 3:20:30 PM?

Are there any setter methods for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2024-09-11 09:24:57

第一个问题的答案“如何以编程方式在 nsdate 中设置小时分钟?
如下:

    NSDate* result;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMinute:0];
    [comps setHour:0];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    result = [gregorian dateFromComponents:comps];
    [comps release];

The answer to your first question "how to set hour minute in nsdate programatically?"
is following:

    NSDate* result;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMinute:0];
    [comps setHour:0];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    result = [gregorian dateFromComponents:comps];
    [comps release];
等数载,海棠开 2024-09-11 09:24:57

如果您需要及时计算特定的真实实例,例如 2010 年 10 月 23 日下午 3:20:30,那么您需要使用 NSCalendar,它可以让您分解日期并进行与日期相关的计算。

警告的话。日期和时间的计算极其复杂和详细。预计需要花费几个小时来学习 API。

If you need to calculate specific real instances in time e.g. 3:20:30 PM Oct 23, 2010, then you want to use NSCalendar which lets you decompose dates and do date related calculations.

Word of warning. Date and time calculations are surprisingly complex and detailed. Expect to spend a couple of hours learning the API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文