- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我真的需要帮助。编码结果为2:0:0
,格式设置为hh:mm:ss
。我希望结果为 2:00:00
(当分秒小于 10
时,在分秒前添加 0
)。
NSDateFormatter *test = [[NSDateFormatter alloc] init];
[test setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [test dateFromString:@"18:00:00"];
NSDate *date2 = [test dateFromString:@"20:00:00"];
NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int uintFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents* differenceComponents = [gregorian components:uintFlags fromDate:date1 toDate:date2 options:0];
NSLog(@"%d:%d:%d",[differenceComponents hour],[differenceComponents minute],[differenceComponents second]);
如何做到这一点?
最佳答案
使用 %02ld
说明符记录,例如:
NSLog(@"%ld:%02ld:%02ld",[differenceComponents hour],[differenceComponents minute],[differenceComponents second]);
输出:
2:00:00
另外创建 NSStrings
如下:
NSString *theString = [NSString stringWithFormat:@"%ld:%02ld:%02ld",[differenceComponents hour],[differenceComponents minute],[differenceComponents second]];
NSLog(@"%@",theString);
关于ios - NSDateComponents 的 NSLog 分秒 - 如何显示前导零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12206223/
我正在创建一个每周日历 View ,其中一部分是计算所显示的一周的第一天。我还希望保存的日期具有第一秒的时间,因此我试图确保日期的 hh:mm:ss 均为 00:00:00。 我有以下代码,在大多数情
我正在编写一些代码来获取服务器上的图像。图像文件名采用 yyyyMMdd_HHmm 格式(UTC),每 3 小时生成一次(1200,1500,1800,..等)。当代码启动时,我获取当前 UTC 日期
当我到达 2011 年 11 月 6 日时,我似乎无法弄清楚为什么这一天没有改变。我所做的只是遍历这些日子。任何帮助将不胜感激。这是我的代码: NSCalendar* calendar = [[NSC
我已经从Zaph阅读以下答案 iPhone - Correct way for getting current date and time for a given place / timezone a
如果您要运行此代码: // January 16th 2015 10:20 AM in Amsterdam var date = NSDate(timeIntervalSince1970: 14214
我有这段代码 NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCa
我正在使用多个 NSDateComponents 来显示一个时钟,其各个值在整个屏幕上被打散,但我遇到了格式化问题。 秒数显示为 1-60 而不是 00-59,小时数为 24 小时制而不是 12 小时
我有以下代码。当我在我的 iOS 3.1.3 设备上运行它时(克服它!)它按预期工作,但是当我在模拟器(iOS 5 和 4.3)上运行它时它会关闭 1 分 15 秒(9:01:15 ).这是因为我的代
我正在尝试获取两个日期之间的差异,它应该以天为单位显示。 NSLog(@"%@", [NSDate date]); NSDateFormatter *formatter = [[NSDateForma
我正在创建一个 NSDate。为什么一天总是少一天? 这是代码: NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar
为什么这给我错误的日期? NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCa
我一直在使用 Swift 和 Xcode 6.3.2 开发一个时钟应用程序,以下代码构建和运行得很好。 // Get current time let date = NSDate() let cale
我正在尝试从 2 个变量获取数据:hours = "08" 和 minutes = 30。我是这样做的: let calendar = NSCalendar(identifier: NSCalenda
我的问题是处理下面代码片段中给出的 NSDateComponents 操作的时间配置文件。 我有一个迭代大量 NSDate 对象的方法 该方法的一部分要求我放弃每个日期的小时、分钟和秒数。 为此,我有
我试图在 NSDateComponents 中设置时间,我编写了以下代码 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIden
我不明白为什么我不能使用 NSDateComponents 正确地更改 NSDate 的小时,我会这样做: NSDateComponent components = [calendar compone
一年有多少周?因为我在网上搜索过,我发现一年总共有 52.1 周,但是如果我在 Xcode 中这样做: NSCalendar *gregorian = [[NSCalendar alloc]
我希望显示来自 NSDate 对象的月数。 //Make Date Six Months In The Future NSCalendar *calendar = [[NSCalendar alloc
如果我有两个 nsdatecomponent 并且我想知道第一个对象中的 TIME 是否大于第二个。 例子: if (nsdatecomponentObject1 > nsdatecomponentO
我一直在使用 Swift 和 Xcode 6.3.2 开发一个时钟应用程序,以下代码构建和运行得很好。 // Get current time let date = NSDate() let cale
我是一名优秀的程序员,十分优秀!