1

I am trying to convert fajerTime to NSDate. When I compile the project the dateValue is nil. Any idea how to fix this issue?

if prayerCommingFromAdan.id == 0 && prayerCommingFromAdan.ringToneId != 0{
    //   NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:"NotificationIdentifier", object: nil)

    let fajerTime = "\(prayer0.time[0...1]):\(prayer0.time[3...4])" as String
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MM-dd-yyyy"
    dateFormatter.timeZone = NSTimeZone.localTimeZone()

    // convert string into date
    let dateValue = dateFormatter.dateFromString(fajerTime) as NSDate!
    print(dateValue)

    var dateComparisionResult:NSComparisonResult = NSDate().compare(dateValue)

    if dateComparisionResult == NSComparisonResult.OrderedDescending {
        addNotificationAlarm(year, month: month, day: day, hour: prayer0.time[0...1], minutes: prayer0.time[3...4], soundId: prayerCommingFromAdan.ringToneId, notificationBody: "It is al fajr adan")                    
    }
Luke Van In
  • 5,215
  • 2
  • 24
  • 45
  • Possible duplicate of [How to convert string to NSDate in iphone?](http://stackoverflow.com/questions/6288371/how-to-convert-string-to-nsdate-in-iphone) – Yuchen Jul 24 '16 at 13:17
  • Show content of `fajerTime` – Arsen Jul 24 '16 at 13:20
  • The problem might be the format of `fajerTime`. It looks like `fajerTime` is a time string, e.g. `12:34`, whereas the date formatter is configured to accept string containing a month, day and year, e.g. `24-07-2016`. You either need to format `fajerTime` according to the `MM-dd-yyyy` format, or otherwise configure the date formatter to accept a time, e.g. `dateFormatter.dateFormat = "hh:mm"`. – Luke Van In Jul 24 '16 at 13:21
  • `fajerTime` seems to be just a time (h:m). Where do get the date (day, month, year) from? – vadian Jul 24 '16 at 13:22

2 Answers2

0

The problem seems be the format of fajerTime. It looks like fajerTime is a time string, e.g. 12:34, whereas the date formatter is configured to accept string containing a month, day and year, e.g. 24-07-2016.

You need to format fajerTime to include the year, month and day, as well as the time. Also configure the date formatter to accept the full date and time.

Assuming prayer0 is an array, you will also need to combine the elements into a string, using joinWithSeparator.

e.g.

let hours = prayer0.time[0...1].joinWithSeparator("")
let minutes = prayer0.time[3...4].joinWithSeparator("")
let fajerTime = "\(month)-\(day)-\(year) \(hours):\(minutes)"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy hh:mm"
dateFormatter.timeZone = NSTimeZone.localTimeZone()

// convert string into date
let dateValue = dateFormatter.dateFromString(fajerTime) as NSDate!
Luke Van In
  • 5,215
  • 2
  • 24
  • 45
0

Please follow example (Swift 3):

let dateStr = "2016-01-15 20:10:01 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let myDate = dateFormatter.date(from: dateStr)

Keep in mind:

  • Date format e.g. "yyyy-MM-dd HH:mm:ss Z" must match the date string pattern

  • In your case, your date string contains only time, yet, your date formatter contains only date

  • When using dateFormatter.date() there is no need to cast it to Date as it returns a Date: enter image description here

Helpful website for date formats:

Roy K
  • 3,319
  • 2
  • 27
  • 43