I'm experiencing a really strange issue that sounds more like a system bug.
I want to format a date using only Hour and Minute information and, if necessary, display AM/PM.
Here is my code:
extension NSDate {
func localizedStringTime()->String {
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.dateFormat = "HH:mm"
}
}
As you can see i'm using HH and not hh and as stated on Apple documentation it should automatically add AM/PM if user chooses 12h format:
The representation of the time may be 13:00. In iOS, however, if the
user has switched 24-Hour Time to Off, the time may be 1:00 pm.
I found that it works perfectly on my Device (I'm based in Europe) but it doesn't work on USA Devices and on Simulator, where even if user selects 12h format it still returning the 24h format.
I've also tried to change my Region to United State but from my device it still work correctly.
Do you see any problem with my code?
Anyway, this problem is also
You can use the localized string from date function like this...
extension NSDate {
func localizedStringTime()->String {
return NSDateFormatter.localizedStringFromDate(self, dateStyle: NSDateFormatterStyle.NoStyle, timeStyle: NSDateFormatterStyle.ShortStyle)
}
}
Swift 3, 4, 5:
extension Date {
var localizedStringTime: String {
return DateFormatter.localizedString(from: self, dateStyle: .none, timeStyle: .short)
}
}
Related
I’m working on a date/time app that does things that Apple didn’t build into its OSs and bundled applications.
I’m leveraging built-in classes like Date and DateFormatter for certain parts, and I’m getting some odd results for dates on the Hebrew calendar localized in Hebrew.
If I request the standard full, long, medium, and short formats, everything is fine. But when I supply a template for DateFormatter to use, something weird happens.
That middle row in the three-date complication (containing the date on the Gregorian, Hebrew, and Muslim calendars), is wrong.
It should be יום א׳, ז׳ בחשון תשפ״א, because the convention is for Hebrew dates in Hebrew to use Hebrew numerals.
The relevant code that produces the Hebrew date:
We first start with setting the locale and time zone of our specialized Hebrew and Islamic calendar class’s date formatter.
if localeIdentifier == "" {
self.dateFormatter.locale = Locale.current
} else {
self.dateFormatter.locale = Locale(identifier: localeIdentifier)
}
self.dateFormatter.timeZone = timeZone
After some code dealing with generating the correct time (not really used in a complication), we set the DateFormatter’s dateStyle or date format based one the value of an enum (“majorDateFormat”); if we are supposed to use a template (case .localizedLDML), we use a specific string (“dateGeekFormat”) as the template.
In this case, I have specified a format with weekday, day, month, and year narrow enough to squeeze into a watchOS complication without losing information. We then generate the date string for the Date fixedNow.
switch majorDateFormat {
case .localizedLDML:
let dateFormat = DateFormatter.dateFormat(fromTemplate:dateGeekFormat, options: 0, locale: self.dateFormatter.locale)!
self.dateFormatter.setLocalizedDateFormatFromTemplate(dateFormat)
case .none:
self.dateFormatter.dateStyle = .none
case .full:
self.dateFormatter.dateStyle = .full
case .long:
self.dateFormatter.dateStyle = .long
case .medium:
self.dateFormatter.dateStyle = .medium
case .short:
self.dateFormatter.dateStyle = .short
default:
self.dateFormatter.dateStyle = .full
} // switch majorDateFormat
let dateString = self.dateFormatter.string(from: fixedNow)
Now the weird things are:
For the Islamic calendar in Arabic, Arabic numerals are correctly produced.
This code actually used to correctly work for the Hebrew calendar in Hebrew.
I had no problems with this on watchOS 5 on my old series 0 Apple Watch, but I recently upgraded to an Apple Watch SE running watchOS 7 and discovered this problem.
Has anyone else seen such a problem before? Is this a bug on Apple’s part, or is there something I’ve missed?
Thanks in advance for any help anyone can provide.
Here's how you can produce "יום א׳, ז׳ בחשון תשפ״א"
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .hebrew)
formatter.locale = Locale(identifier: "he")
formatter.dateStyle = .short // must come before date format below
formatter.setLocalizedDateFormatFromTemplate("c dd MMMM yyyy")
print(formatter.string(from: Date())) // יום א׳, ז׳ בחשון תשפ״א
When I try to log the current date:
print(NSDate())
or
print(Date())
(in Swift 3)
Or any date object, it shows the wrong time. For example, it's about 16:12 now, but the above displayed
2016-10-08 20:11:40 +0000
Is my date in the wrong time zone? How do I fix my date to have the correct time zone?
Why is that, and how to I fix it? How do I easily display an arbitrary date in my local time zone, either in print statements or in the debugger?
(Note that this question is a "ringer" so that I can provide a simple Swift 3/Swift 2 Date/NSDate extension that lets you easily display any date object in your local time zone.
NSDate (or Date in Swift ≥ V3) does not have a time zone. It records an instant in time all over the world.
Internally, date objects record the number of seconds since the "epoch date", or Midnight on January 1, 2001 in Greenwich Mean Time, a.k.a UTC.
We normally think of dates in our local time zone.
If you log a date using
print(NSDate())
The system displays the current date, but it expresses it in UTC/Greenwich Mean Time. So the only place the time will look correct is in that time zone.
You get the same issue in the debugger if you issue the debugger command
e NSDate()
This is a pain. I personally wish iOS/Mac OS would display dates using the user's current time zone, but they don't.
EDIT #2:
An improvement on my previous use of localized string that makes it a little easier to use is to create an extension to the Date class:
extension Date {
func localString(dateStyle: DateFormatter.Style = .medium, timeStyle: DateFormatter.Style = .medium) -> String {
return DateFormatter.localizedString(from: self, dateStyle: dateStyle, timeStyle: timeStyle)
}
}
That way you can just use an expression like Date().localString(), or if you want to only print the time, you can use Date().localString(dateStyle:.none)
EDIT:
I just discovered that NSDateFormatter (DateFormatter in Swift 3) has a class method localizedString. That does what my extension below does, but more simply and cleanly. Here is the declaration:
class func localizedString(from date: Date, dateStyle dstyle: DateFormatter.Style, timeStyle tstyle: DateFormatter.Style) -> String
So you'd simply use
let now = Date()
print (DateFormatter.localizedString(
from: now,
dateStyle: .short,
timeStyle: .short))
You can pretty much ignore everything below.
I have created a category of the NSDate class (Date in swift 3) that has a method localDateString that displays a date in the user's local time zone.
Here is the category in Swift 3 form: (filename Date_displayString.swift)
extension Date {
#nonobjc static var localFormatter: DateFormatter = {
let dateStringFormatter = DateFormatter()
dateStringFormatter.dateStyle = .medium
dateStringFormatter.timeStyle = .medium
return dateStringFormatter
}()
func localDateString() -> String
{
return Date.localFormatter.string(from: self)
}
}
And in Swift 2 form:
extension NSDate {
#nonobjc static var localFormatter: NSDateFormatter = {
let dateStringFormatter = NSDateFormatter()
dateStringFormatter.dateStyle = .MediumStyle
dateStringFormatter.timeStyle = .MediumStyle
return dateStringFormatter
}()
public func localDateString() -> String
{
return NSDate.localFormatter.stringFromDate(self)
}
}
(If you prefer a different date format it's pretty easy to modify the format used by the date formatters. It's also straightforward to display the date and time in any timezone you need.)
I would suggest putting the appropriate Swift 2/Swift 3 version of this file in all of your projects.
You can then use
Swift 2:
print(NSDate().localDateString())
Swift 3:
print(Date().localDateString())
A simple way to correct the Date for your timezone would be to use TimeZone.current.secondsFromGMT()
Something like this for a local timestamp value for example:
let currentLocalTimestamp = (Int(Date().timeIntervalSince1970) + TimeZone.current.secondsFromGMT())
One day, the app worked. The next day I updated to Xcode 11 and now the app crashes with "unexpectedly found nil" on line 27 (when executing line 15) in the picture.
I asked my co-worker who doesn't yet have Xcode 11, and his doesn't crash. we are on the same branch/commit...everything.
Any advice? any way around this?
My code:
// ticket.timeOrdered == "2019-10-03 22:54:57 +0000"
let ticketDate = ticket.timeOrdered.asCrazyDate.timeIntervalSince1970
extension String {
var asCrazyDate: Date {
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz"
dateFormatterGet.timeZone = .current
return dateFormatterGet.date(from: self)!
}
}
The date format string is incorrect. The +zzzz is not an acceptable format. See the timezone related sections of the table in Date Format Patterns. The + shouldn’t be there. And zzzz is for long descriptions of the time zone (e.g. “Pacific Daylight Time”). You can verify this by using the same formatter to build a string from Date() and you’ll see that it’s not resulting in the +0000 like you might have expected.
The latest SDK’s date formatter is no longer as forgiving regarding these sorts of format string errors as the previous versions were. But rather than reverting your Xcode version, you really should just fix that date format string. For example, you could use Z instead of +zzzz, which will correctly interpret the +0000 (or whatever) as the time zone portion of the string.
A few other suggestions, if you don’t mind:
You don’t need asCrazyDate in this example. There’s no point in getting a date, using string interpolation to build the string representation, and then using a formatter to convert the string back to a date (which you originally started with). You can just use the Date directly:
func getDate() -> TimeInterval {
return Date().timeIntervalSince1970
}
Date formatters are notoriously computationally intensive to create, and if you’re using this computed property a lot, that can really affect performance. It’s better to instantiate date formatters once, if possible.
If you’re trying to build some invariant date string for some reason, it’s better to use something like ISO8601DateFormatter. So don’t build your date strings using string interpolation, and don’t build your own formatter.
let formatter = ISO8601DateFormatter()
let now = Date()
let string = formatter.string(from: now) // not "\(now)"
let date = formatter.date(from: string)
print(now, string, date)
If you’re stuck with this date format (perhaps you’ve already stored dates using this string format), you can use the custom dateFormat string, if you must. But as Technical Q&A 1480 suggests, you might want to set the locale (and I’d suggest setting the timeZone, too, so your date strings are comparable).
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
how can i return a NSDate in a predefined time zone from a string
let responseString = "2015-8-17 GMT+05:30"
var dFormatter = NSDateFormatter()
dFormatter.dateFormat = "yyyy-M-dd ZZZZ"
var serverTime = dFormatter.dateFromString(responseString)
println("NSDate : \(serverTime!)")
the above code returns the time as
2015-08-16 18:30:00 +0000
The date format has to be assigned to the dateFormat property of the date formatter instead.
let date = NSDate.date()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let str = dateFormatter.stringFromDate(date)
println(str)
This prints the date using the default time zone on the device. Only if you want the output according to a different time zone then you would add for example
Swift 3.*
dateFormatter.timeZone = NSTimeZone(name: "UTC")
Swift 4.*
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
also refer link http://www.brianjcoleman.com/tutorial-nsdate-in-swift/
how can i return a NSDate in a predefined time zone?
You can't.
An instance of NSDate does not carry any information about timezone or calendar. It just simply identifies one point in universal time.
You can interpret this NSDate object in whatever calendar you want. Swift's string interpolation (the last line of your example code) uses an NSDateFormatter that uses UTC (that's the "+0000" in the output).
If you want the NSDate's value as a string in the current user's calendar you have to explicitly set up a date formatter for that.
Swift 4.0
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
If you always have the same time zone for the input string, you can create two date formatters to output the local time zone (or a specified one):
let timeFormatterGet = DateFormatter()
timeFormatterGet.dateFormat = "h:mm a"
timeFormatterGet.timeZone = TimeZone(abbreviation: "PST")
let timeFormatterPrint = DateFormatter()
timeFormatterPrint.dateFormat = "h:mm a"
// timeFormatterPrint.timeZone = TimeZone(abbreviation: "EST") // if you want to specify timezone for output, otherwise leave this line blank and it will default to devices timezone
if let date = timeFormatterGet.date(from: "3:30 PM") {
print(timeFormatterPrint.string(from: date)). // "6:30 PM" if device in EST
} else {
print("There was an error decoding the string")
}
The number 1 means 1 regardless of language. Yet in English it's spelled as one, in Spanish it's una, in Arabic it wahid, etc.
Similarly 123982373 seconds pass 1970 is going to reflect differently in different timezones or calendar formats, but's all still 123982373 seconds passed 1970
The difference between 3 seconds and 7 seconds is 4 seconds. That doesn't require a calendar. Neither you need a calendar/timezone to know the difference in time between these two Epoch times 1585420200 and 1584729000
Dates are just a timeInterval from January 1, 1970 (midnight UTC/GMT). Dates also happen to have a string representation.
Repeating Nikolia's answer, Swift's default string interpolation (2015-08-16 18:30:00 +0000) uses a DateFormatter that uses UTC (that's the "+0000" in the output).
Calendars with the use of timezones give us a contextual representation that is just easier to understand than trying to calculate the difference between two gigantic numbers.
Meaning a single date (think of a single timeInterval since 1970) will have a different string interpretations per calendar. On top of that a calendar will itself vary based on time zones
I highly recommend that you go and play around with this Epoch converter site and see how selecting a different timezone will cause the string representations for the same moment/date/timeInterval to change
I also recommend to see this answer. Mainly this part:
Timezone is just an amendment to the timestamp string, it's not considered by the date formatter.
To consider the time zone you have to set the timeZone of the formatter
dateFormatter.timeZone = TimeZone(secondsFromGMT: -14400)
I am updating the question to be more precise, thank you for the feedback already.
So I am setting a cell for a tableview and displaying the time of the event on the cell itself and to do so am using Parse. The date is downloading correctly from parse as evidenced by the first println, but when I make the label text equal the dateToString of the when date (which is the one from parse) it throws it off by several hours for some reason. The simulator however displays the right time on the status bar and the phones gps is set to EST as well... So I am very confused.
Here is the entire function:
func configureCell(object : PFObject)-> Void {
eventNameText.text = (object.objectForKey("name") as? String)!
var when = (object.objectForKey("when") as? NSDate)!
numEchoesText.text = String(stringInterpolationSegment: object.objectForKey("numEchoes")!)
println(when)
var formatter = NSDateFormatter()
formatter.dateFormat = "EEEE, h:mm a"
parseid = object.objectId!
if (contains(echoes, parseid)) {
echoImage.setImage(imageEchoed, forState: .Normal)
}
else {
echoImage.setImage(imageUnechoed, forState: .Normal)
}
var now = NSDate()
whenText.text = formatter.stringFromDate(when)
println(formatter.stringFromDate(now))
println(formatter.stringFromDate(when))
self.object = object
}
And here is the result:
2015-05-31 23:43:00 +0000
Tuesday, 9:30 AM
Sunday, 7:43 PM
Please note that the first time is correct but the datetostring is interpreting it as the third line. I added a middle println to display the current date in order to show that the datetostring is working correctly for the now date.
Thanks in advance for any help!!
Edit: updated answer based on new details.
Your trouble is because of the timezone. NSDate does not store any time zone information.
println(when)
--> 2015-05-31 23:43:00 +0000 // notice the timezone is 0000 / GMT
Adding a timezone to the NSDateFormatter will help you see what's going on behind the scene:
var formatter = NSDateFormatter()
formatter.dateFormat = "EEEE, h:mm a z"
println(formatter.stringFromDate(when))
--> Sunday, 7:43 PM xyz // Depends on the timezone on your device
The two represents the same point in time, but expressed in different timezones. To solve this, you need to set the timezone of the formatter to GMT:
var formatter = NSDateFormatter()
formatter.dateFormat = "EEEE, h:mm a z"
formatter.timeZone = NSTimeZone.init(name:"GMT")
println(formatter.stringFromDate(when))
--> Sunday, 11:43 PM GMT
You can remove the timezone identifier now that you get it correct. Date & time manipulation in Cocoa / iOS are a lot more complicated than other languages (Java, C#, Javascript, etc.) Sometimes I wish Apple just created a NSDate class with methods to add & subtract date/time components, switch timezone, etc. I can dream on.
Take a look at NSDateFormatter and how it works. If I do understand your question clearly, I think this should work.
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm"
dateFormatter.stringFromDate(parseDateObject)
2015-05-31 23:43:00 +0000 means "the 31st of May 2015 at 23:43:00 at +0000 from UTC".
According to your question "The simulator however displays the right time on the status bar and the phones gps is set to EST".
If you convert that date from UTC to EST you get "Sunday, 7:43 PM".
Your NSDate is therefore being interpreted perfectly.
Making reasonable assumptions about the Parse API, it looks like somebody has imported your data incorrectly.