Getting back a date from a string

The problem there is that Y is for weekOfYear. You have to use “dd-MM-yyyy”. Btw don’t forget to set your date formatter locale to “en_US_POSIX” . If you’re working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale … Read more

NSDateFormatter: Date according to currentLocale, without Year

I think you need to take a look at: + (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale As per the docs: Returns a localized date format string representing the given date format components arranged appropriately for the specified locale. Return Value A localized date format string representing the date format components given in template, arranged appropriately … Read more

NSDateFormatter and current language in iOS11

This isn’t a problem with NSDateFormatter, it’s a change in how iOS 11 supports localization. Under iOS 11, [NSLocale currentLocale] only returns languages supported by your app’s localizations. If your app only supports English (as the base localization), then no matter what language the user selects on the device, currentLocale will always return English. Under … Read more

Swift 3.0 : Convert server UTC time to local time and vice-versa

I don’t know what’s wrong with your code.But looks too much unnecessary things are there like you’re setting calendar, fetching some elements from string. Here is my small version of UTCToLocal and localToUTC function. But for that you need to pass string in specific format. Cause I’ve forcly unwrapped date objects. But you can use … Read more

How to convert NSDate in to relative format as “Today”,”Yesterday”,”a week ago”,”a month ago”,”a year ago”?

For simplicity I’m assuming that the dates you are formatting are all in the past (no “tomorrow” or “next week”). It’s not that it can’t be done but it would be more cases to deal with and more strings to return. You can use components:fromDate:toDate:options: with whatever combination of date components you are looking for … Read more

Using DateFormatter on a Unix timestamp

You can convert unixTimestamp to date using Date(timeIntervalSince1970:). let unixTimestamp = 1480134638.0 let date = Date(timeIntervalSince1970: unixTimestamp) If you want to display date in string with specific formate than you can use DateFormatter like this way. let date = Date(timeIntervalSince1970: unixtimeInterval) let dateFormatter = DateFormatter() dateFormatter.timeZone = TimeZone(abbreviation: “GMT”) //Set timezone that you want dateFormatter.locale … Read more