In objective-c, the following code results in the UTC date time information using the date
API.
NSDate *currentUTCDate = [NSDate date]
Xcode 9 • Swift 4 (also works Swift 3.x)
extension Formatter {
// create static date formatters for your date representations
static let preciseLocalTime: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "HH:mm:ss.SSS"
return formatter
}()
static let preciseGMTTime: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "HH:mm:ss.SSS"
return formatter
}()
}
extension Date {
// you can create a read-only computed property to return just the nanoseconds from your date time
var nanosecond: Int { return Calendar.current.component(.nanosecond, from: self) }
// the same for your local time
var preciseLocalTime: String {
return Formatter.preciseLocalTime.string(for: self) ?? ""
}
// or GMT time
var preciseGMTTime: String {
return Formatter.preciseGMTTime.string(for: self) ?? ""
}
}
Playground testing
Date().preciseLocalTime // "09:13:17.385" GMT-3
Date().preciseGMTTime // "12:13:17.386" GMT
Date().nanosecond // 386268973
This might help you also formatting your dates:
a date is independant of any timezone, so use a Dateformatter and attach a timezone for display:
swift:
let date = NSDate()
let dateFormatter = NSDateFormatter()
let timeZone = NSTimeZone(name: "UTC")
dateFormatter.timeZone = timeZone
println(dateFormatter.stringFromDate(date))
objC:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeZone:timeZone];
NSLog(@"%@", [dateFormatter stringFromDate:date]);
At the moment (with the latest changes to Swift), NSDate.date()
is not longer available.
Instead you just need to initialize NSDate
and it gets the current date and time.
To try it, in a playground:
var d = NSDate()
d
and you will get:
Oct 22, 2014, 12:20 PM"
NSDate
is a specific point in time without a time zone. Think of it as the number of seconds that have passed since a reference date. How many seconds have passed in one time zone vs. another since a particular reference date? The answer is the same.
Depending on how you output that date (including looking at the debugger), you may get an answer in a different time zone.
If they ran at the same moment, the values of these are the same. They're both the number of seconds since the reference date, which may be formatted on output to UTC or local time. Within the date variable, they're both UTC.
Objective-C:
NSDate *UTCDate = [NSDate date]
Swift:
let UTCDate = NSDate.date()
To explain this, we can use a NSDateFormatter in a playground:
import UIKit
let date = NSDate.date()
// "Jul 23, 2014, 11:01 AM" <-- looks local without seconds. But:
var formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
let defaultTimeZoneStr = formatter.stringFromDate(date)
// "2014-07-23 11:01:35 -0700" <-- same date, local, but with seconds
formatter.timeZone = NSTimeZone(abbreviation: "UTC")
let utcTimeZoneStr = formatter.stringFromDate(date)
// "2014-07-23 18:01:41 +0000" <-- same date, now in UTC
The date output varies, but the date is constant. This is exactly what you're saying. There's no such thing as a local NSDate.
As for how to get microseconds out, you can use this (put it at the bottom of the same playground):
let seconds = date.timeIntervalSince1970
let microseconds = Int(seconds * 1000) % 1000 // chops off seconds
To compare two dates, you can use date.compare(otherDate)
.
The documentation says that the date
method returns a new date set to the current date and time regardless of the language used.
The issue probably sits somewhere where you present the date using NSDateFormatter
. NSDate
is just a point on a time line. There is no time zones when talking about NSDate
. I made a test.
print(NSDate())
Output: 2014-07-23 17:56:45 +0000
NSLog(@"%@", [NSDate date]);
Output: 2014-07-23 17:58:15 +0000
Result - No difference.
let date = Date()
print(date) // printed date is UTC
If you are using playground, use a print statement to check the time. Playground shows local time until you print it. Do not depend on the right side panel of playground.
This code gives date in UTC. If you need the local time, you should call the following extension with timezone as Timezone.current
extension Date {
var currentUTCTimeZoneDate: String {
let formatter = DateFormatter()
formatter.timeZone = TimeZone(identifier: "UTC")
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return formatter.string(from: self)
}
}
For UTC time, use it like: Date().currentUTCTimeZoneDate