Convert Date to Integer in Swift
Date
to Int
// using current date and time as an example
let someDate = Date()
// convert Date to TimeInterval (typealias for Double)
let timeInterval = someDate.timeIntervalSince1970
// convert to Integer
let myInt = Int(timeInterval)
Doing the Double
to Int
conversion causes the milliseconds to be lost. If you need the milliseconds then multiply by 1000 before converting to Int
.
Int
to Date
Including the reverse for completeness.
// convert Int to TimeInterval (typealias for Double)
let timeInterval = TimeInterval(myInt)
// create NSDate from Double (NSTimeInterval)
let myNSDate = Date(timeIntervalSince1970: timeInterval)
I could have also used `timeIntervalSinceReferenceDate` instead of `timeIntervalSince1970` as long as I was consistent. This is assuming that the time interval is in seconds. Note that Java uses milliseconds.
Note
- For the old Swift 2 syntax with
NSDate
, see this answer.
Convert Date String to Int Swift
Answers by @alex_p and @mixel are correct, but it's also possible to do it with Swift split
function:
let time = "7:30"
let components = time.characters.split { $0 == ":" } .map { (x) -> Int in return Int(String(x))! }
let hours = components[0]
let minutes = components[1]
How to Convert UNIX epoch time to Date and time in ios swift
update: Xcode 8.2.1 • Swift 3.0.2 or later
You need to convert it from milliseconds dividing it by 1000:
let epochTime = TimeInterval(1429162809359) / 1000
let date = Date(timeIntervalSince1970: epochTime) // "Apr 16, 2015, 2:40 AM"
print("Converted Time \(date)") // "Converted Time 2015-04-16 05:40:09 +0000\n"
swift: convert long/int64 to date
Just let JSONDecoder
do the job by using the appropriate date decoding strategy
let json = """
{"id" : 1, "date" : 1529704800000}
"""
struct Example : Decodable {
let id : Int
let date : Date
}
let data = Data(json.utf8)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .millisecondsSince1970
do {
let result = try decoder.decode(Example.self, from: data)
print(result) // Example(id: 1, date: 2018-06-22 22:00:00 +0000)
} catch {
print(error)
}
Note: Don't make an asynchronous task synchronous. Learn to understand asynchronous data processing and use a completion handler.
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)
Convert NSDate to an Integer
Use dd
in setDateFormat:
.
This will give only date but in string.
After this you can convert the string to integer, with the help of integerValue
.
Edit:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd"];
NSString *stringFromDate = [formatter stringFromDate:[NSDate date]];
NSInteger integerDate = [stringFromDate integerValue];
Related Topics
Wkwebview Auto Fill Login Form Swift 2
Accessor Gives the Wrong Value in Swift 1.2/2.0 Release Build Only
Core Data Predicate Not Working
How to Encode an Unmanaged<Seckey> to Base64 to Send to Another Server
Getting a Let Value Outside a Function
Swift: When Should I Use "Var" Instead of "Let"
Appending Tuples to an Array of Tuples
Get Signed Integer from Swift String of Binary
Singleton and Init with Parameter
How to Change the Current Day's Hours and Minutes in Swift
Uiview Background Color in Swift
How to Save Document to "Files" App in Swift
Setting Backgroundcolor of Custom Nsview