GVKun编程网logo

从NSTimeInterval迅速转换为时,分,秒,毫秒(system.nanotime转换成秒)

32

本文将分享从NSTimeInterval迅速转换为时,分,秒,毫秒的详细内容,并且还将对system.nanotime转换成秒进行详尽解释,此外,我们还将为大家带来关于cocoa–Trivial“[N

本文将分享从NSTimeInterval迅速转换为时,分,秒,毫秒的详细内容,并且还将对system.nanotime转换成秒进行详尽解释,此外,我们还将为大家带来关于cocoa – Trivial“[NSTimer scheduledTimerWithTimeInterval:重复:阻止:]:无法识别的选择器”错误、IOS NSTimeInterval使用案例详解、ios – NSDate dateWithTimeIntervalSince1970:我必须从NSTimeInterval中减去1800秒才能显示正确的值、ios – NSTimeInterval到unix时间戳的相关知识,希望对你有所帮助。

本文目录一览:

从NSTimeInterval迅速转换为时,分,秒,毫秒(system.nanotime转换成秒)

从NSTimeInterval迅速转换为时,分,秒,毫秒(system.nanotime转换成秒)

我的代码在这里:

func stringFromTimeInterval(interval:NSTimeInterval) -> NSString {    var ti = NSInteger(interval)    var ms = ti * 1000    var seconds = ti % 60    var minutes = (ti / 60) % 60    var hours = (ti / 3600)      return NSString(format: "%0.2d:%0.2d:%0.2d",hours,minutes,seconds,ms)}

在输出中毫秒数会给出错误的结果。请给出一个如何正确找到毫秒数的想法。

答案1

小编典典

Swift支持对浮点数进行余数计算,因此我们可以使用% 1

var ms = Int((interval % 1) * 1000)

如:

func stringFromTimeInterval(interval: TimeInterval) -> NSString {  let ti = NSInteger(interval)  let ms = Int((interval % 1) * 1000)  let seconds = ti % 60  let minutes = (ti / 60) % 60  let hours = (ti / 3600)  return NSString(format: "%0.2d:%0.2d:%0.2d.%0.3d",hours,minutes,seconds,ms)}

结果:

stringFromTimeInterval(12345.67)                   "03:25:45.670"

斯威夫特4:

extension TimeInterval{        func stringFromTimeInterval() -> String {            let time = NSInteger(self)            let ms = Int((self.truncatingRemainder(dividingBy: 1)) * 1000)            let seconds = time % 60            let minutes = (time / 60) % 60            let hours = (time / 3600)            return String(format: "%0.2d:%0.2d:%0.2d.%0.3d",hours,minutes,seconds,ms)        }    }

用:

self.timeLabel.text = player.duration.stringFromTimeInterval()

cocoa – Trivial“[NSTimer scheduledTimerWithTimeInterval:重复:阻止:]:无法识别的选择器”错误

cocoa – Trivial“[NSTimer scheduledTimerWithTimeInterval:重复:阻止:]:无法识别的选择器”错误

移动到10.12 / Sierra和 Xcode 8.1之后,我正在敲打一个奇怪的错误:

+[NSTimer scheduledTimerWithTimeInterval:repeats:block:]: 
     unrecognized selector sent to class 0x7fff78f1fa88

重现这一点的最小代码(创建新项目的默认设置)是:

//  AppDelegate.m
//

#import "AppDelegate.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (strong,nonatomic) NSTimer * timer;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:10 
                           repeats:YES 
                             block:^(NSTimer * _Nonnull timer) 
   {
        NSLog(@"Ping from %@",timer);
    }];
}

链接包括(核心)基础类和’all_load’.必须是完全无足轻重的东西 – 但不是它的本质.

任何和所有帮助表示赞赏.

谢谢,

DW.

解决方法

[NSTimer scheduledTimerWithTimeInterval:重复:block:]是iOS 10.0方法.您是否可能尝试在iOS 9.x上运行它?

https://developer.apple.com/reference/foundation/nstimer/2091889-scheduledtimerwithtimeinterval?language=objc

IOS NSTimeInterval使用案例详解

IOS NSTimeInterval使用案例详解

一 ios 获取时间间隔

想在程序开始或者进入某个界面 ,到结束程序或退出某个界面,获取到这个持续时间. 获取到这个时间还需要转化一个取得时分秒.
-(NSString *)getCurrentTime

{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *dateTime = [formatter stringFromDate:[NSDate date]];

    self.startTime = dateTime;

    return startTime;

}

date1代表开始时间,在开始计时的地方调用 [self getCurrentTime]; 在结束时的方法里写如下代码:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date1 = [formatter dateFromString:startTime];

NSDate *date2 = [NSDate date];

NSTimeInterval aTimer = [date2 timeIntervalSinceDate:date1];

 

int hour = (int)(aTimer/3600);

int minute = (int)(aTimer - hour*3600)/60;

int second = aTimer - hour*3600 - minute*60;

NSString *dural = [NSString stringWithFormat:@"%d时%d分%d秒", hour, minute,second];

二 我想把它转换成分钟和秒。

比如我有:“326.4”秒,我想把它转换成下面的字符串: “5:26”。 什么是实现这一目标的最佳方法是什么?

1. 伪代码:

minutes = floor(326.4/60)
seconds = round(326.4 - minutes * 60)

2. 简述 从布赖恩・拉姆齐答案是更方便的,如果你只是想转换为分钟。 如果你想Cocoa API的为你做它和转换您不仅分钟,但也给天,月,星期等,...我认为这是一个更通用的方法 使用

(NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts“返回,作为NSDateComponents两者之间的差异提供的日期”。从API 创建2 NSDate的 CodeGo.net,其区别是你要转换。 (如果你的2 NSDate的你不需要做这一步,你甚至不需要的 让你的quotes从NSDateComponents 示例代码

// The time interval 
NSTimeInterval theTimeInterval = 326.4;
// Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1]; 
// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
NSLog(@"Conversion: %dmin %dhours %ddays %dmoths",[conversionInfo minute], [conversionInfo hour], [conversionInfo day], [conversionInfo month]);
[date1 release];
[date2 release];

已知问题 太多的只是一个转换,你是对的,但是这是API如何工作的。 我的建议:如果你要管理你的NSDate和NSCalendar,该API将做艰苦的工作适合你。 

3. ,作为一个堆栈处女...我不知道如何回答布赖恩・拉姆齐的回答... 使用圆不会为59.5和59.99999之间第二值工作。第二个值将是60在此期间。使用TRUNC而不是...

double progress;
int minutes = floor(progress/60);
int seconds = trunc(progress - minutes * 60);

4. 布赖恩・拉姆齐的代码,去pseudofied:

- (NSString*)formattedStringForDuration:(NSTimeInterval)duration
{
 NSInteger minutes = floor(duration/60);
 NSInteger seconds = round(duration - minutes * 60);
 return [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
}

5. 所有这些看起来比他们需要!这里有一个简短而亲切的方式来转换的时间间隔为小时,分钟和秒:

NSTimeInterval timeInterval = 326.4;
long seconds = lroundf(timeInterval); // Modulo (%) operator below needs int or long
int hour = seconds / 3600
int mins = (seconds % 3600) / 60;
int secs = seconds % 60;

请注意,当你把一个浮点数转换为整数,你得到楼()自动完成,但你可以,如果要是让你感觉更好:-)它添加到的前两个 

6. 因为它本质上是一个双... 60.0划分和提取的组成部分和小数部分。 的组成部分,将是分钟的整数。 再乘以小数部分按60.0。 其结果将是剩下秒。

到此这篇关于IOS NSTimeInterval使用案例详解的文章就介绍到这了,更多相关IOS NSTimeInterval使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能感兴趣的文章:
  • IOS之WebSocket框架Starscream案例详解
  • iOS实现从通讯录中选择联系人
  • iOS实现贝塞尔曲线动画
  • iOS实现日历行程的增删改查
  • 使用vue3.x+vite+element-ui+vue-router+vuex+axios搭建项目

ios – NSDate dateWithTimeIntervalSince1970:我必须从NSTimeInterval中减去1800秒才能显示正确的值

ios – NSDate dateWithTimeIntervalSince1970:我必须从NSTimeInterval中减去1800秒才能显示正确的值

我是iOS的新手,作为我的第一个重大项目,我正在尝试编写一个音乐播放器应用程序.

我正在使用AVAudioPlayer,它将当前文件的持续时间报告为NSTimeInterval.要以mm:ss格式显示,我使用的是NSDate和NSDateFormatter.最方便的方法是使用dateWithTimeIntervalSince1970:方法.但是,由于我不知道的原因,该方法为NSTimeInterval增加了1800秒(30分钟).

为什么这样做?如何从NSTimeInterval中减去1800秒来更清楚地解决问题?

我不知道它是否相关,但我的时区是GMT 5:30.也许这是造成问题的不寻常时区?

解决方法

看到这个问题: how can i define NSTimeInterval to mm:ss format in iphone?

您可以算术计算小时和分钟,然后将它们格式化为Nsstring.这比使用NSDate和NSDateFormatter更快.

ios – NSTimeInterval到unix时间戳

ios – NSTimeInterval到unix时间戳

我从cmmotionmanager获取CMDeviceMotion对象. CMDeviceMotion的一个属性是时间戳,表示为NSTimeInterval(double).根据文档,这允许“亚毫秒”时间戳精度.
[motionManager startDeviceMotionUpdatesToQueue:motionQueue withHandler:^(CMDeviceMotion *motion,NSError *error) { 
  NSLog(@"Sample: %d Timestamp: %f ",counter,motion.timestamp);
}

不幸的是,NSTimeInterval是自上次设备启动以来计算的,对以原始形式使用它提出了重大挑战.

有没有人有一个工作代码将此NSTimeInterval转换为类似时间戳(UTC时区)的Unix?

谢谢!

解决方法

在将磁力计值与CoreMotion事件进行比较时,我遇到了类似的问题.如果要转换这些NSTimeIntervals,您只需要计算一次偏移量:
// during initialisation

// Get NSTimeInterval of uptime i.e. the delta: Now - boottime
NSTimeInterval uptime = [nsprocessInfo processInfo].systemUptime;

// Now since 1970
NSTimeInterval NowTimeIntervalSince1970 = [[NSDate date] timeIntervalSince1970];

// Voila our offset
self.offset = NowTimeIntervalSince1970 - uptime;

今天关于从NSTimeInterval迅速转换为时,分,秒,毫秒system.nanotime转换成秒的分享就到这里,希望大家有所收获,若想了解更多关于cocoa – Trivial“[NSTimer scheduledTimerWithTimeInterval:重复:阻止:]:无法识别的选择器”错误、IOS NSTimeInterval使用案例详解、ios – NSDate dateWithTimeIntervalSince1970:我必须从NSTimeInterval中减去1800秒才能显示正确的值、ios – NSTimeInterval到unix时间戳等相关知识,可以在本站进行查询。

本文标签: