0

I am parsing this DateString 01/06/2017 14:31 GMT+10:00 into Date using date format dd/MM/yyyy HH:mm z but some times it gives nil values. and some times it works correctly.

Can you suggest correct Date format for this date string.

I am using swift 3.0 and Xcode 8.3.

My codes are :

static func getTimestampFromDateString(dateString:String) -> Int64{
    let separated = dateString.components(separatedBy: " ")
    let currentUser = User.getLoggedInUserDataModel()
    let finalDateStr = "\(String.getString(separated[0])) \(String.getString(separated[1])) \(String.getString(currentUser.timeZoneOffset))"

    let date = convertStringToDate(dateString: finalDateStr, dateFormat: "dd/MM/yyyy HH:mm z")
    let timestamp = Int64(date.timeIntervalSince1970) 
    return timestamp
}
static func convertStringToDate(dateString:String,dateFormat:String)->Date{
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = dateFormat
    let date:Date = dateFormatter.date(from: dateString)!
    return date
}
aircraft
  • 25,146
  • 28
  • 91
  • 166
  • Put a nil check before unwrapping the optional value (before let date:Date = dateFormatter.date(from: dateString)!) – jegadeesh Jun 06 '17 at 04:53
  • its not working at all – Divya Saraswati Jun 06 '17 at 05:10
  • Yes, Im executing your code now.. just now saw only date is coming correctly using that fromatter – jegadeesh Jun 06 '17 at 05:12
  • this code is working in 24 hr format but only not working in 12 hr format for some devices – Divya Saraswati Jun 06 '17 at 05:24
  • Possible duplicate of https://stackoverflow.com/questions/40692378/dateformatter-doesnt-return-date-for-hhmmss – set the date formatter locale to "en_US_POSIX". – Martin R Jun 06 '17 at 05:33
  • @DivyaSaraswati the date format mentioned in the question will not cater to a 12-hour format. Hence the crash. If you want to use a 12-hour format, you have to replace `z` with `a` in your `date format` – Malik Jun 06 '17 at 06:48

3 Answers3

0

The problem is not your date format. The problem is that you are force unwrapping an optional, which is causing the crash. If you properly unwrapped the optional value, you would prevent the crash. See optional unwrapping: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID330

Kyle Redfearn
  • 2,172
  • 16
  • 34
0

You can resolve the problem in two steps : 1: Check the date formate of finalDateStr, if finalDateStr is same as "dd/MM/yyyy HH:mm z" then you can convert string in this formate. 2: Check nil before force unwrapping the dateFormatter.date(from: dateString)!

example

 if let date = dateFormatter.date(from: dateString)!

{


// got the date


}else {
  // check finalDateStr formate

}
An Kit
  • 308
  • 1
  • 2
  • 9
Optimus
  • 800
  • 5
  • 16
0

The date format "dd/MM/yyyy HH:mm z" works good for me . By the way , I am using this code below

extension String
{
     func  toDate( dateFormat format  : String) -> Date
    {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        if let date = dateFormatter.date(from: self)
        {
            return date
        }
        print("Invalid arguments ! Returning Current Date . ")
        return Date()
    }
}

and here is how i called . .

print("01/06/2017 14:31 GMT+10:00".toDate(dateFormat: "dd/MM/yyyy HH:mm z"))
roy
  • 6,685
  • 3
  • 26
  • 39