Iphone: How to Get Current Milliseconds

iPhone: How to get current milliseconds?

[[NSDate date] timeIntervalSince1970];

It returns the number of seconds since epoch as a double. I'm almost sure you can access the milliseconds from the fractional part.

Date to milliseconds and back to date in Swift

I don't understand why you're doing anything with strings...

extension Date {
var millisecondsSince1970:Int64 {
Int64((self.timeIntervalSince1970 * 1000.0).rounded())
}

init(milliseconds:Int64) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)
}
}


Date().millisecondsSince1970 // 1476889390939
Date(milliseconds: 0) // "Dec 31, 1969, 4:00 PM" (PDT variant of 1970 UTC)

Timestamp (milliseconds) in Swift

On 32-bit platforms, CUnsignedLong is a 32-bit integer, which is not large
enough to hold the number 1397016000000. (This is different from Java, where
long is generally a 64-bit integer.)

You can use UInt64 or NSTimeInterval (a type alias for Double), which is what the
NSDate methods use.

iOS Swift - Get the Current Local Time and Date Timestamp

For saving Current time to firebase database I use Unic Epoch Conversation:

let timestamp = NSDate().timeIntervalSince1970

and For Decoding Unix Epoch time to Date().

let myTimeInterval = TimeInterval(timestamp)
let time = NSDate(timeIntervalSince1970: TimeInterval(myTimeInterval))

Convert DateTime in millisecond iOS

Following code for getting date and time into Millisecond.

NSTimeInterval timeInMiliseconds = [[NSDate date] timeIntervalSince1970];

For more detail refer apple documentation.

----Edited----

NSString *dateString = @"21-04-2015 14:23";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSTimeInterval timeInMiliseconds = [dateFromString timeIntervalSince1970]*1000;

How do I get current playing time, CMTime in milliseconds in AVPlayer?

okay,first of all, the value you want is millisecond not seconds

So,you can just use CMTimeGetSeconds(<#CMTime time#>) to get Seconds
then,if you want millisecond , use seconds / 1000.f for float or double value

for CMTime calculating use CMTime method
CMTimeMultiplyByRatio(<#CMTime time#>, <#int32_t multiplier#>, <#int32_t divisor#>)
just do this --> CMTimeMultiplyByRatio(yourCMTimeValue, 1, 1000)

Apple's Doc

@function   CMTimeMultiplyByRatio
@abstract Returns the result of multiplying a CMTime by an integer, then dividing by another integer.
@discussion The exact rational value will be preserved, if possible without overflow. If an overflow
would occur, a new timescale will be chosen so as to minimize the rounding error.
Default rounding will be applied when converting the result to this timescale. If the
result value still overflows when timescale == 1, then the result will be either positive
or negative infinity, depending on the direction of the overflow.

If any rounding occurs for any reason, the result's kCMTimeFlags_HasBeenRounded flag will be
set. This flag will also be set if the CMTime operand has kCMTimeFlags_HasBeenRounded set.

If the denominator, and either the time or the numerator, are zero, the result will be
kCMTimeInvalid. If only the denominator is zero, the result will be either kCMTimePositiveInfinity
or kCMTimeNegativeInfinity, depending on the signs of the other arguments.

If time is invalid, the result will be invalid. If time is infinite, the result will be
similarly infinite. If time is indefinite, the result will be indefinite.


@result (time * multiplier) / divisor

Swift full date with milliseconds

Updated for Swift 3

let d = Date()
let df = DateFormatter()
df.dateFormat = "y-MM-dd H:mm:ss.SSSS"

df.string(from: d) // -> "2016-11-17 17:51:15.1720"

When you have a Date d, you can get the formatted string using a NSDateFormatter. You can also use a formatter to turn a string date based on your format into a Date

See this chart for more on what dateFormat can do http://waracle.net/iphone-nsdateformatter-date-formatting-table/



Related Topics



Leave a reply



Submit