From dateFromString to stringFromDate - ios

I have a date in String that arrives in the following format:
"2015-11-15 14:16:15 +0000"
and I want to display it in a localised format and even calculate the age.
To get to NSDate I use this code:
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss +SSSS"
let birthDate = self.dateFormatter.dateFromString(userProfile["birthdate"] as! String)
Then to display the date (in a short format) I change the dateFormatter
dateFormatter.dateFormat = "yyyy-MM-dd"
let birthDateShort = self.dateFormatter.stringFromDate(birthDate!)
I could of course just pull out the text from the initial string if I didn't need the NSDate format for other reasons. But it still seems like a long roundtrip , is this the "correct" method?

This is exactly the right thing to do. And it's not a "long roundtrip". A string is just a string: just a representation, a bunch of letters / glyphs for a human being to read. A date is serious calendar-related piece of information. You need that date as a central pivot point for doing anything calendrically meaningful. And that's exactly what you're doing. NSDateFormatter is provided exactly so you can do the very kind of thing you are doing.

Related

How to convert HH:mm:ss only to am/pm in swift?

So I have a time that's in string format HH:mm:ss (08:30:00 for example). I want to be able to convert it to 8:30 AM.
Normally it would be easy if I had the year,month,day to go along so it would be simple to do the conversions but in this case I don't need it.
My original plan was to get current Date() then assign the time to it then convert it to the new format of H:mm a but I haven't been able to do this successfully. Meaning I have only been able to convert it to a Date object that has year 1/1/2000 and the time is set in UTC.
Am I overthinking this? Is there an easier way without doing any conversions? I just want to be able to display the time in H:mm a format without altering any timezones.
You need to create a Date with the specified time value. For this, I'd simple create a new instance of Date and then use Calendar to set the time to the desired value
var date = Date()
let cal = Calendar.current
// Please note, this returns an optional, you will need to deal
// with, for demonstration purposes, I've forced unwrapped it
date = cal.date(bySettingHour: 8, minute: 30, second: 0, of: date)!
From there, you make use of a DateFormatter to format the result to a String
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm a"
formatter.string(from: date)
Now, if you're starting with a String value (of HH:mm:ss), you can actually use a DateFormatter to parse it, for example...
let value = "08:30:00"
let parser = DateFormatter()
parser.dateFormat = "HH:mm:ss"
// Again, this is forced unwrapped for demonstration purposes
let parsedDate = parser.date(from: value)!

Xcode 11 broke DateFormatter?

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)

Putting system date into a text field in swift

I'm new to swift and would appreciate some help with string manipulation. I'm trying to get the current date off NSDate and put it into a text field for an app I'm working on. I tried to use NSDateFormatter to put the ios system date into the international form or dd-MM-yyyy, but I just keep getting all these errors and nothing works. I could use the American date format, I just really need it to work. I don't really know swift that much, but I know that other tutorials I tried to follow on stack overflow directed me to put some code in the view controller using NSDate. I worked on some other tutorials and tried to make them do what I needed to and this is the result. It used to create a date and timestamp but I tried to cut the parts out that deal with time. I think I just made it worse.
func convertDateFormatter(date: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
guard let date = dateFormatter.date(from: date) else {
assert(false, "no date from string")
return ""
}
dateFormatter.dateFormat = "dd-MM-yyyy"
let timeStamp = dateFormatter.string(from: date)
return timeStamp
}
My version of swift doesn't recognize NSDate, it wants to change it to just Date, I don't know how it affects how I am supposed to go about doing this. I changed it to just Date in the code and it still doesn't work.
In addition, yesterday my mobile apps teacher and I tried to equate a custom variable and the text field, but it does not work.
var UIDateStamp = UITextField().self
I could be wording my search incorrectly but I have searched this same query all the different ways I could come up with, but every solution I have tried thus far gives me a lot of errors that my coding class and I cannot solve.
I would greatly appreciate help with this issue.
If you need to system date they you need create function without parameter.
Swift 3
func convertDateFormatter() -> String {
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy" // change format as per needs
let result = formatter.string(from: date)
return result
}
If you want a date format depending on the current locale use the timeStyle and dateStyle properties.
This code – as computed property – returns M/d/yy for the US locale
var timeStamp : String {
let formatter = DateFormatter()
formatter.timeStyle = .none
formatter.dateStyle = .short
return formatter.string(from: Date())
}
A date style medium returns MMM d, yyyy

convert date to specific timezone iOS swift [duplicate]

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)

NSDateFormatter still parsing instead having incorrect format

Having some problems parsing date. I have an array of supported formats and once I receive the date (string) from API, I try to parse it iterating through the formats until I get a valid NSDate object.
A snippet from Xcode Playground --
let dateString = "02/06/1987" // --> want to parse into this Feb 6, not Jun 2
let dateFormatIncorrect = "dd.MM.yyyy"
let dateFormatCorrect = "MM/dd/yyyy"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = dateFormatIncorrect
let date = dateFormatter.dateFromString(dateString)! // "Jun 2, 1987, 12:00 AM"
dateFormatter.dateFormat = dateFormatCorrect
let date2 = dateFormatter.dateFromString(dateString)! // "Feb 6, 1987, 12:00 AM"
Why does it parse the date even though the format is clearly incorrect for a given string? Could not find anything in the docs regarding date formatter ignoring separators.
I realise the proper solution would be to have a fixed format returned from API but was wondering what is happening here?
Thanks.
It seems that NSDateFormatter is extremely lenient when parsing a date string.
Unfortunately, I could not find a reference for this, but even with
dateFormatIncorrect = "'aaa'dd'bbb'MM'ccc'yyyy'ddd'"
the date string "02/06/1987" is successfully parsed. There is a lenient property,
but that is false by default, and setting it explicitly makes no difference.
As a workaround, you could convert the parsed date back to a string, and only if
the result is equal to the original string, the date is accepted:
extension NSDateFormatter {
func checkedDateFromString(string : String) -> NSDate? {
if let date = self.dateFromString(string) {
if self.stringFromDate(date) == string {
return date
}
}
return nil
}
}
Using this custom extension,
dateFormatter.checkedDateFromString(dateString)
returns nil for the incorrect date format.
Generally, if you work with fixed date formats, you should also set the locale
to "en_US_POSIX"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
(see What is the best way to deal with the NSDateFormatter locale "feechur"?). However, this makes no difference for this
particular problem.
Update for Swift 3:
extension DateFormatter {
func checkedDate(from: String) -> Date? {
if let date = date(from: from), string(from: date) == from {
return date
}
return nil
}
}
This could be related to the fact that NSDateFormatter will anyways respects the users settings when using fixed formats
Although in principle a format string specifies a fixed format, by
default NSDateFormatter still takes the user’s preferences (including
the locale setting) into account
So may be the locale defined in your preference uses '/' for separator and satisfies the 'incorrect format'. Even if that is not the case, apple noted in several places that NSDateFormatter might not act consistently. So try setting a fixed locale as below and see if that helps
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
See these links for detail: apple tech note . Note directly related to separators, but that could be related.
Had a similar issue:
NSDateFormatter returns date object from invalid input
Filed a bug report at Apple.
Result: Will not be fixed, as the change could break working code, in addition it is more error tolerant and thus provides some kind of convenience.
Please know that our engineering team has determined that this issue
behaves as intended based on the information provided.
It appears that ICU’s udat_parseCalendar() is very lenient and still
is able to parse even if the string doesn’t exactly match the format.
We understand preferring that the formatter return nil in these cases
but (1) there’s no easy way for us to know that the input string
doesn’t match the format since ICU allows it and doesn’t throw an
error and (2) suddenly returning nil in these cases would almost
certainly be a bincompat issue.
In my case I had the option to either modify the unit tests and be more tolerant in case of invalid input or have an additional checkup (based on the recommended approach, which is the accepted answer for the post) whether the resulting NSDate's string fits to the input string.

Resources