nstimeinterval

Convert date to timestamp in iOS

耗尽温柔 提交于 2019-12-01 09:27:27
How do you convert any given date to milliseconds? For example, 2014-01-23 to timestamp conversion. NSDate *date = [dateFormatter dateFromString:@"2014-01-23"]; NSLog(@"date=%@",date); NSTimeInterval interval = [date timeIntervalSince1970]; NSLog(@"interval=%f",interval); NSDate *methodStart = [NSDate dateWithTimeIntervalSince1970:interval]; [dateFormatter setDateFormat:@"yyyy/mm/dd "]; NSLog(@"result: %@", [dateFormatter stringFromDate:methodStart]); Output result: 1970/30/01 Have it a try. "mm" stands for minute while "MM" stands for month. NSDateFormatter * dateFormatter = [[NSDateFormatter

Adding and Subtracting times in Swift

。_饼干妹妹 提交于 2019-12-01 04:09:54
问题 I've written some of this in pseudo code because I don't know the syntax for it. I'd like to have the timeLeftLabel.text reflect how many hours, minutes, and seconds are left until the 6 hours are up. My biggest problem is that I don't know how to add and subtract times. Can anyone help me? var timer = NSTimer() func timerResults() { let theDate = NSDate() var endTime = theDate //+ 6 hours let timeLeft = endTime //- theDate timeLeftLabel.text = "\(timeLeft)" } @IBOutlet weak var timeLeftLabel

dateWithTimeIntervalSince1970 not returning correct date

僤鯓⒐⒋嵵緔 提交于 2019-11-30 21:40:18
I have the following method below that is meant to retrieve and convert a unixTimeStamp from an API call to a NSDate object that I can easily manipulate and use. For some reason, this returns wrong values. An example would be when the unixTimeStamp is 1385152832, the date SHOULD be Fri, 22 Nov 2013 20:40:31 GMT November 22, 2013 at 3:40:31 PM EST but instead spits out: 45852-09-07 08:13:52 EST. Does anyone know why this would happen? -(NSDate *)messageDate { NSTimeInterval unixTimeStamp = [[self messageDateString] doubleValue]; NSDate *messageDate = [NSDate dateWithTimeIntervalSince1970

NSTimeInterval to unix timestamp

妖精的绣舞 提交于 2019-11-30 20:33:37
I'm getting CMDeviceMotion objects from CMMotionManager . One of the properties of the CMDeviceMotion is timestamp, which is expressed as a NSTimeInterval (double). This allows for "sub millisecond" timestamp precision, according to documentation. [motionManager startDeviceMotionUpdatesToQueue:motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error) { NSLog(@"Sample: %d Timestamp: %f ",counter, motion.timestamp); } Unfortunately, NSTimeInterval is calculated since last device boot, posing significant challenges to using it in its raw form. Does anyone have a working code to convert

Subtracting two NSDate objects [duplicate]

假如想象 提交于 2019-11-30 06:00:57
Possible Duplicate: How to Get time difference in iPhone I´m getting date and time from a JSON feed. I need to find the difference between the date I´m getting from the feed and today´s date and time. Any suggestions how I can do this? I know I need to subtract the current date with the date I get from the feed, but I don´t know how to do it. Ex: Date from feed: Date: 2011-06-10 15:00:00 +0000 Today: Date: 2011-06-10 14:50:00 +0000 I need to display that the difference is ten minutes. Thanks! Create two NSDate objects from the strings using NSDate's -dateWithString: , then get the difference

dateWithTimeIntervalSince1970 not returning correct date

我们两清 提交于 2019-11-30 05:24:33
问题 I have the following method below that is meant to retrieve and convert a unixTimeStamp from an API call to a NSDate object that I can easily manipulate and use. For some reason, this returns wrong values. An example would be when the unixTimeStamp is 1385152832, the date SHOULD be Fri, 22 Nov 2013 20:40:31 GMT November 22, 2013 at 3:40:31 PM EST but instead spits out: 45852-09-07 08:13:52 EST. Does anyone know why this would happen? -(NSDate *)messageDate { NSTimeInterval unixTimeStamp = [

Subtracting two NSDate objects [duplicate]

☆樱花仙子☆ 提交于 2019-11-29 05:31:49
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to Get time difference in iPhone I´m getting date and time from a JSON feed. I need to find the difference between the date I´m getting from the feed and today´s date and time. Any suggestions how I can do this? I know I need to subtract the current date with the date I get from the feed, but I don´t know how to do it. Ex: Date from feed: Date: 2011-06-10 15:00:00 +0000 Today: Date: 2011-06-10 14:50:00 +0000

conversion from NSTimeInterval to hour,minutes,seconds,milliseconds in swift

安稳与你 提交于 2019-11-28 16:49:52
My code is here: 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) } in output the milliseconds give wrong result.Please give an idea how to find milliseconds correctly. Matthias Bauch Swift supports remainder calculations on floating-point numbers, so we can use % 1 . var ms = Int((interval % 1) * 1000) as in: func stringFromTimeInterval(interval: TimeInterval) -> NSString { let ti =

Convert a Date (absolute time) to be sent/received across the network as Data in Swift?

醉酒当歌 提交于 2019-11-28 06:20:49
问题 I am looking for a Swifty way to generate a timestamp. My macOS app logs some data and stamps it with the time the data was created. The data will then be sent across the network (as Data ) to be reconstructed on an iPad. Is there any Swift class that will work to generate the timestamp? NSDate? NSTimeIntervalSince1970? CFAbsoluteTimeGetCurrent() The requirements are: Store the timestamp in as few bytes as possible (pref. Int ) Have some semblance to real Earth time (I'd rather not generate

Find difference in seconds between NSDates as integer using Swift

独自空忆成欢 提交于 2019-11-28 03:31:40
I'm writing a piece of code where I want to time how long a button was held down. To do that I recorded an NSDate() when the button was pressed, and tried using the timeIntervalSinceDate function when the button was released. That seems to work but I can't find any way to print the result or switch it to an integer. var timeAtPress = NSDate() @IBAction func pressed(sender: AnyObject) { println("pressed") timeAtPress = NSDate() } @IBAction func released(sender: AnyObject) { println("released") var elapsedTime = NSDate.timeIntervalSinceDate(timeAtPress) duration = ??? } I've seen a few similar