Is there any way to print full date with milliseconds?
For example, I'm doing this:
print("\(NSDate())")
But I'm just get this:
2016-05-09 22:07:19 +0000
How can I get the milliseconds too in the full date?
Is there any way to print full date with milliseconds?
For example, I'm doing this:
print("\(NSDate())")
But I'm just get this:
2016-05-09 22:07:19 +0000
How can I get the milliseconds too in the full date?
Updated for Swift 3
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "y-MM-dd H:mm:ss.SSSS"
formatter.string(from: date) // -> "2016-11-17 17:51:15.1720"
When you have a Date
date
, you can get the formatted string using a DateFormatter
. You can also use a formatter to turn a string date based on your format into a Date
See this chart for more on what dateFormat can do http://waracle.net/iphone-nsdateformatter-date-formatting-table/
ss.SSS
at the end instead of ss +SSSS
. –
Haulm yyyy
, not Y
. –
Haulm y
and Y
. They are two different years. 99.99% of the time a person wants y
, not Y
. And what makes you think the OP wants the milliseconds formatted like a timezone? They are simply showing the standard output of printing a date (which shows the timezone offset by default) and asks how to add milliseconds to that output. That doesn't mean they want the milliseconds formatted like a timezone. –
Haulm Y
is wrong. it will return the year of a week-based calendar. A subtle error that even apple got wrong before. –
Sarinasarine Y
is wrong: gitlab.com/snippets/19107 It just creates a date from a string and than creates a string form the date — and the year is off by 1. if you change the Y
from the format to y
the result is correct. –
Sarinasarine y-MM-dd H:mm:ss.SSS
(only 3 S
) for milliseconds? –
Scintillation Swift 5 to/from Timestamp String Extension
extension String {
static func timestamp() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyyMMdd'T'HHmmss.SSSS"
let now = Date.now
return String(format: "%@", dateFormatter.string(from: now))
}
func tad2Date() -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyyMMdd'T'HHmmss.SSSS"
return dateFormatter.date(from: self)
}
}
iOS 15.0+ / macOS 12.0+
You can now also use the locale-aware formatted()
API to display milliseconds using the .secondFraction(.fractional(3))
modifier:
let date = Date()
print(date.formatted(Date.FormatStyle().month(.twoDigits).day(.twoDigits).year().hour().minute().second(.twoDigits).secondFraction(.fractional(3)).timeZone(.iso8601(.short))))
// "18.04.2024, 11:50:14,696 +0200" (my current locale is "DE")
Now that's some long string. You can break it down and store your format somewhere for re-use:
let preferredFormat = Date.FormatStyle()
.weekday(.abbreviated)
.month(.twoDigits)
.day(.twoDigits)
.year()
.hour()
.minute()
.second(.twoDigits)
.secondFraction(.fractional(3))
.timeZone(.iso8601(.short))
.locale(.init(identifier: "US"))
print(date.formatted(preferredFormat))
// "Thu, 04/18/2024, 11:49:44.133 +0200"
© 2022 - 2024 — McMap. All rights reserved.
NSDateFormatter
with the properdateFormat
. Printing the actual date object gives you the string representation for the date in UTC, not necessarily the time zone you are interested in. – ChillyNSDate().timeIntervalSince1970
to get milliseconds – ShebeentimeIntervalSince1970
returns seconds not milliseconds. – Teredo