Answers for "convert string into time ios swift"

3

swift stirng to date

let isoDate = "2016-04-14T10:44:00+0000"

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from:isoDate)!

let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .hour], from: date)

let finalDate = calendar.date(from:components)
Posted by: Guest on December-21-2020
0

swift converting time string to number

//https://stackoverflow.com/questions/50950092/calculating-the-difference-between-two-dates-in-swift
//https://stackoverflow.com/a/51050452/13171606
//first link solution:
extension Date {

    static func -(recent: Date, previous: Date) -> (month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?) {
        let day = Calendar.current.dateComponents([.day], from: previous, to: recent).day
        let month = Calendar.current.dateComponents([.month], from: previous, to: recent).month
        let hour = Calendar.current.dateComponents([.hour], from: previous, to: recent).hour
        let minute = Calendar.current.dateComponents([.minute], from: previous, to: recent).minute
        let second = Calendar.current.dateComponents([.second], from: previous, to: recent).second

        return (month: month, day: day, hour: hour, minute: minute, second: second)
    }

}

//implimentation in controler
let interval = Date() - updatedDate
print(interval.day)
print(interval.month)
print(interval.hour)

//also see this link ->https://stackoverflow.com/questions/33202466/convert-date-string-to-int-swift
Posted by: Guest on February-26-2021

Code answers related to "convert string into time ios swift"

Code answers related to "Swift"

Browse Popular Code Answers by Language