从一天的一小部分创建时间字符串

发布于 2024-10-07 05:02:12 字数 180 浏览 0 评论 0原文

所有 NSDate 的东西都值得吸收!我有一个 float ,它是一天 24 小时的小数部分,我想将其转换为 hh:mm AM/PM 格式的 NSString

我敢打赌,如果你知道自己在做什么的话,这只是一句俏话……;)

All the NSDate stuff is a lot to take in! I have a float that is the decimal fraction of a 24 hour day and I want to convert it to an NSString in the format hh:mm AM/PM.

I bet it's a one-liner if you know what you're doing... ;)

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

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

发布评论

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

评论(1

心病无药医 2024-10-14 05:02:12

也许是这样的?

float day_fraction = 0.45; // This is your input fraction-of-day.

int day_seconds = 24 * 3600 * day_fraction;
NSDate *tod = [NSDate dateWithTimeIntervalSince1970:day_seconds];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mma"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

NSString *dateString = [dateFormat stringFromDate:tod];
NSLog(@"date: %@", dateString);

结果:

date: 10:48AM

Perhaps something like this?

float day_fraction = 0.45; // This is your input fraction-of-day.

int day_seconds = 24 * 3600 * day_fraction;
NSDate *tod = [NSDate dateWithTimeIntervalSince1970:day_seconds];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mma"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

NSString *dateString = [dateFormat stringFromDate:tod];
NSLog(@"date: %@", dateString);

Result:

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