I'm making a function to format string date (like "2018-05-01 18:23:31") to string type of Japanese date format, which is "2018年5月1日". I made a function but when I run it throws me an error saying
Fatal error: Unexpectedly found nil while unwrapping an Optional value
when I print my input string date, it has a value, but while it's converting, I start getting an error. Can anyone tell me why I'm getting nill in this function?
func setTemplate (strDate: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "ydMMM", options: 0, locale: Locale(identifier: "ja_JP"))
print(strDate) // getting this value => "2018-05-01 18:23:31"
let date = dateFormatter.date(from: strDate)! // getting an error in here
let result = dateFormatter.string(from: date)
return result
}
I guess I'm trying to get rid of time in the string and it somehow throws me an error, but not sure...
You need to first parse your string then you can get a localized string from the resulting date:
let strDate = "2018-05-01 18:23:31"
func setTemplate (strDate: String) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.locale = .init(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
guard let date = dateFormatter.date(from: strDate) else { return nil }
let locale = Locale(identifier: "ja_JP")
dateFormatter.locale = locale
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "ydMMM", options: 0, locale: locale)
return dateFormatter.string(from: date)
}
setTemplate(strDate: strDate) // "2018年5月1日"
Related
I'm getting a response from api that is date string, it's format changes sometimes so I need to format it dynamically,
Here's my code
import Foundation
func format(from: String?, fromFormat: String, to: String) -> String {
if from == nil { return "" }
let inputDateFormatter = DateFormatter()
inputDateFormatter.dateFormat = fromFormat
let date = inputDateFormatter.date(from: from ?? "")
let outputDateFormatter = DateFormatter()
outputDateFormatter.dateFormat = to
if let date = date {
return outputDateFormatter.string(from: date)
}
return "not formatted"
}
let strFromApi = "2020-12-22"
print(format(from: strFromApi, fromFormat: "yyyy-MM-dd", to: "d MMM yyyy"))
As you can see, I have a code there that can format successfully, the problem here is that strFromApi variable was came from api, and is changing between 2020-12-22 and 2020-12-22 00:00:00, when it changes to 2020-12-22 00:00:00, my current code can't format it anymore.
Question: How can I format it even the given format from the server has time?
You can create two date formatters, one with time and another without it and use nil coalescing operator to provide a fallback in case the first one fails. Regarding the date format when returning your final string you should respect the user's device locale and settings. Just use dateStyle and timeStyle when displaying a date to the user:
extension Formatter {
static let date: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
static let dateAndTime: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return formatter
}()
static let localizedDate: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
return formatter
}()
}
extension Date {
var localizedDate: String { Formatter.localizedDate.string(from: self) }
}
func formatted(from string: String?) -> String {
guard
let string = string,
let date = Formatter.dateAndTime.date(from: string) ??
Formatter.date.date(from: string)
else { return "" }
return date.localizedDate
}
let strFromApi = "2020-12-22 00:00:00" // "2020-12-22" //
formatted(from: strFromApi) // "Dec 22, 2020"
I read a date from UserDefaults.standard as string in the following format:
"2020-11-06T05:20:20+0100"
I try to convert this string to a date using the following code
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "de_DE")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let dateString: String! = UserDefaults.standard.string(forKey: "timeShared")
let date = dateFormatter.date(from: dateString)
However, the result is always nil. Using let date = dateFormatter.date(from: dateString)! explicitly causes a fatal error.
Any ideas what to do?
Changing the former code to
dateFormatter.dateFormat = "\"yyyy-MM-dd'T'HH:mm:ssZ\""
returns a neatly formatted date subsequently. The error was that the date read from UserDefaults included "".
May be UserDefaults.standard.string(forKey: "timeShared") have no value or the value is not in string format and you are doing force unwrapping in let dateString: String! = UserDefaults.standard.string(forKey: "timeShared") here that making the fatal error.
Better add a conditional if, then use the dateString.
Refer the below code.
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "de_DE")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
if let dateString: String = UserDefaults.standard.string(forKey: "timeShared") {
let date = dateFormatter.date(from: dateString)
print(date)
} else {
print("Data in UserDefaults: \(UserDefaults.standard.string(forKey: "timeShared"))")
}
I have a string form 2019-06-22T01:11:08.996Z but I can't work out why my date formatter won't convert it to an NSDate object. I have tried using yyyy-MM-dd'T'HH:mm:ssZ. But I always get nil.
What would be the correct format for form date as above?
Try following code to get the date:
let dateStr = "2019-06-22T01:11:08.996Z"
let date = dateFrom(dateString: dateStr, withDateFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ", andLocale: "en_US_POSIX")
print(date)
--
func dateFrom(dateString: String, withDateFormat dateFormat: String, andLocale locale: String) -> Date? {
let dateformatter = DateFormatter()
dateformatter.locale = Locale(identifier: locale)
dateformatter.dateFormat = dateFormat
return dateformatter.date(from: dateString)
}
Please help me to modify this part of code,
if let dateVaule = UserDefaults().value(forKey: SplashSeenDate){
let dateStr = dateVaule as! String
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone.ReferenceType.local
let date = dateFormatter.date(from:dateStr)
if Global.sharedInstance.Splashs != nil && Global.sharedInstance.Splashs?.count != 0{
for (i,splash) in (Global.sharedInstance.Splashs?.enumerated().reversed())!{
if !checkTimeStamp(date: splash.create_timestamp,StoredDate: date!){
Global.sharedInstance.Splashs?.remove(at: i)
}
}
print(Global.sharedInstance.Splashs?.count ?? "-1")
}
}
Honestly speaking, i am very confuse with the date formatter.
when this project launch, it occured crash at
if !checkTimeStamp(date: splash.create_timestamp,StoredDate: date!){
However, I could not replicate this crash... **Only a few user with iOS10 occured this crash.**Please kindly advise, Thanks a lot!
func checkTimeStamp(date: String!,StoredDate: Date) -> Bool {
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier:"en_US_POSIX")
let datecomponents = dateFormatter.date(from: date)
if (datecomponents! >= StoredDate) {
return true
} else {
return false
}
}
You are converting String to Date in two different ways:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone.ReferenceType.local //###Why don't you use `TimeZone.current`?
let date = dateFormatter.date(from:dateStr)
And in checkTimeStamp(date: String!,StoredDate: Date) -> Bool:
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier:"en_US_POSIX")
let datecomponents = dateFormatter.date(from: date)
In your case, lacking this line is critical for your first conversion:
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
You can find many articles discussing about DateFormatter (or NSDateFormatter in old articles) returning nil.
When you use DateFormatter with fixed format like "yyyy-MM-dd HH:mm:ss", you must set locale to Locale(identifier: "en_US_POSIX").
Maybe you'd better define one conversion method (or computed property) to convert String to Date which fits for your app's requirements, and use it everywhere you need String to Date conversion:
extension String {
var toAppDate: Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
dateFormatter.locale = Locale(identifier: "en_US_POSIX") //### <- Do not miss.
return dateFormatter.date(from: self)
}
}
Generally, your code uses too much forced-unwrapping or forced-casting. You can re-write it without using any forced-something:
if
let dateStr = UserDefaults().string(forKey: SplashSeenDate),
let date = dateStr.toAppDate,
let splashes = Global.sharedInstance.Splashs, !splashes.isEmpty
{
//### In most cases, `filter` is faster than repeated `remove`.
Global.sharedInstance.Splashs = splashes.filter {
checkTimeStamp(date: $0.create_timestamp, storedDate: date)
}
print(Global.sharedInstance.Splashs?.count ?? "-1")
}
(Assuming create_timestamp is not Optional, and renamed parameter label StoredDate: to storedDate:.)
Seems like splash.create_timestamp is getting nil and when you are passing in this function checkTimeStamp. It is getting crash. You can prevent the crash by changing date parameter inside checkTimeStamp function as optional and use guard statement before passing inside the function.
I have a date format in String value of "2015-08-27" which is "YYYY-MM-DD". I need to convert this to Date format and change the format to "DD-MMM-YYYY". And change it back to String format again to display. So the end result would be "27-AUG-2015".
I have been searching for codes, but couldn't find one.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-DD"
let date = dateFormatter.dateFromString("2015-08-27")
dateFormatter.dateFormat = "DD-MMM-YYYY"
let goodDate = dateFormatter.stringFromDate(date!)
Its not tested but I hope it will work.
You can create a class helper like DateHelper and class func:
class func convertDateString(dateString : String!, fromFormat sourceFormat : String!, toFormat desFormat : String!) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = sourceFormat
let date = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = desFormat
return dateFormatter.string(from: date!)
}
And use it:(note format you give on question wrong so it will wrong convert): begin format is YYYY-MM-dd and you want convert to dd-MMM-YYYY)
print(DateHelper.convertDateString("2015-08-27", fromFormat: "YYYY-MM-dd", toFormat: "dd-MMM-YYYY"))
Hope this help.
Just used the function in your code(swift 4.2).
public func convertDateFormatter(date: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"//this your string date format
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
dateFormatter.locale = Locale(identifier: "your_loc_id")
let convertedDate = dateFormatter.date(from: date)
guard dateFormatter.date(from: date) != nil else {
assert(false, "no date from string")
return ""
}
dateFormatter.dateFormat = "HH:mm a"///this is what you want to convert format
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
let timeStamp = dateFormatter.string(from: convertedDate!)
print(timeStamp)
return timeStamp
}
Thanks