Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 652 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
conversion from NSTimeInterval to hour,minutes,seconds,milliseconds in swift

#11
Swift 4, without using the `.remainder` (which returns wrong values):

func stringFromTimeInterval(interval: Double) -> NSString {

let hours = (Int(interval) / 3600)
let minutes = Int(interval / 60) - Int(hours * 60)
let seconds = Int(interval) - (Int(interval / 60) * 60)

return NSString(format: "%0.2d:%0.2d:%0.2d",hours,minutes,seconds)
}
Reply

#12
Swift 3 solution for iOS 8+, macOS 10.10+ if the zero-padding of the hours doesn't matter:

func stringFromTime(interval: TimeInterval) -> String {
let ms = Int(interval.truncatingRemainder(dividingBy: 1) * 1000)
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
return formatter.string(from: interval)! + ".\(ms)"
}

print(stringFromTime(interval: 12345.67)) // "3:25:45.670"
Reply

#13
swift 3 version of @hixField answer, now with days and handling previous dates:

extension TimeInterval {
func timeIntervalAsString(_ format : String = "dd days, hh hours, mm minutes, ss seconds, sss ms") -> String {
var asInt = NSInteger(self)
let ago = (asInt < 0)
if (ago) {
asInt = -asInt
}
let ms = Int(self.truncatingRemainder(dividingBy: 1) * (ago ? -1000 : 1000))
let s = asInt % 60
let m = (asInt / 60) % 60
let h = ((asInt / 3600))%24
let d = (asInt / 86400)

var value = format
value = value.replacingOccurrences(of: "hh", with: String(format: "%0.2d", h))
value = value.replacingOccurrences(of: "mm", with: String(format: "%0.2d", m))
value = value.replacingOccurrences(of: "sss", with: String(format: "%0.3d", ms))
value = value.replacingOccurrences(of: "ss", with: String(format: "%0.2d", s))
value = value.replacingOccurrences(of: "dd", with: String(format: "%d", d))
if (ago) {
value += " ago"
}
return value
}

}

Reply

#14
converted into an swift 2 extension + variable format :

extension NSTimeInterval {

func timeIntervalAsString(format format : String = "hh:mm:ss:sss") -> String {
let ms = Int((self % 1) * 1000)
let asInt = NSInteger(self)
let s = asInt % 60
let m = (asInt / 60) % 60
let h = (asInt / 3600)

var value = format
value = value.replace("hh", replacement: String(format: "%0.2d", h))
value = value.replace("mm", replacement: String(format: "%0.2d", m))
value = value.replace("sss", replacement: String(format: "%0.3d", ms))
value = value.replace("ss", replacement: String(format: "%0.2d", s))
return value
}

}

extension String {
/**
Replaces all occurances from string with replacement
*/
public func replace(string:String, replacement:String) -> String {
return self.stringByReplacingOccurrencesOfString(string, withString: replacement, options: NSStringCompareOptions.LiteralSearch, range: nil)
}

}
Reply

#15
for convert hour and minutes to seconds in swift 2.0:

///RETORNA TOTAL DE SEGUNDOS DE HORA:MINUTOS
func horasMinutosToSeconds (HoraMinutos:String) -> Int {

let formatar = NSDateFormatter()
let calendar = NSCalendar.currentCalendar()
formatar.locale = NSLocale.currentLocale()
formatar.dateFormat = "HH:mm"

let Inicio = formatar.dateFromString(HoraMinutos)
let comp = calendar.components([NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: Inicio!)

let hora = comp.hour
let minute = comp.minute

let hours = hora*3600
let minuts = minute*60

let totseconds = hours+minuts

return totseconds
}

Reply

#16
Equivalent in Objective-C, based on the @matthias-bauch answer.

+ (NSString *)stringFromTimeInterval:(NSTimeInterval)timeInterval
{
NSInteger interval = timeInterval;
NSInteger ms = (fmod(timeInterval, 1) * 1000);
long seconds = interval % 60;
long minutes = (interval / 60) % 60;
long hours = (interval / 3600);

return [NSString stringWithFormat:@"%0.2ld:%0.2ld:%0.2ld,%0.3ld", hours, minutes, seconds, (long)ms];
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through