0

So my goal is to convert the time from an NSDate to an integer however, I am unsure of how to go about this. I have tried doing a simple conversion such as Int(...) however it does not work. Is this due to the fact that the string includes AM or PM? I would really appreciate any help towards trying to get the time to be an integer. Thank you.

var firstDate = DateFormatter.localizedString(from: NSDate() as Date, dateStyle: DateFormatter.Style.none, timeStyle: DateFormatter.Style.short)

This is generated in the view did load section. Again any help would be very much appreciated, thank you.

EDIT:

Sorry reading over the question it probably was not particularly clear as to what I'm after. I want to be able to find the difference in time between two different NSDates. This will further be used as a countdown feature. The problem associated with this is that I am unsure of exactly how I am going to subtract two strings. Therefore I figured that if I can change the hours, minutes and seconds to integers, I could then subtract them. What I need assistance with is any way which I can achieve this. Perhaps changing the values to intervals is a bad way of doing this but it was just a thought I had and am open to any suggestions.

1
  • what do you need the integer for? Jan 19, 2017 at 23:46

1 Answer 1

-1

It depends on what is the integer supposed to represent. Seconds can be obtained by getting time interval from another date. For example:

let date1 = Date()
let date2 = Date.init(timeInterval: 12345, since: date1)

let timeInterval = Int(date2.timeIntervalSince(date1))
// timeInterval = 12345 and is an int, the unit is seconds

So, you don't subtract strings, but work with Date or NSDate directly...

7
  • Sorry the question wasn't particularly clear, I have added a edit which explains in more detail what I'm after
    – imjonu
    Jan 20, 2017 at 0:47
  • Ok, I updated my answer based on your updated question.
    – MirekE
    Jan 20, 2017 at 0:52
  • OK so its slightly off still. It is kind of hard to explain exactly what I want but what I need is to determine the time date two is from date one. I have an example here of how this would be used. date one = 10:00am date two = 10:23am (date two will be an updating function every second and will be the current date and time. date one will be the time when the app was started) Therefore (date one - date two) = time since app started
    – imjonu
    Jan 20, 2017 at 1:26
  • If I understand you correctly, this is what my sample code does. Time2 - time1 expressed as an integer in seconds.
    – MirekE
    Jan 20, 2017 at 2:07
  • Yes however date two isn't the current time? Your sample code works off a set integer. All I need is to get a way to convert date two, which will be the current time, to an integer. In doing so I can take the current time off the previous time (date one) to find the timeInterval. Your sample code kind of works in reverse where it already. Eg. intial time is 0 then you add 12345. Therefore date 2 - date 1 will give you 12345 again?
    – imjonu
    Jan 20, 2017 at 2:25

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.