-1

The source code is come from

Hijri (islamic) calendar in swift التاريخ الهجري

How can I convert string date to NSDate?

I just put them together but it has some mistake with the output

    let dateFormatter = NSDateFormatter()

    dateFormatter.dateFormat = "YYYY-MM-DD"

    let GregorianDate = dateFormatter.dateFromString("\(Yeartext1.text!)-\(Monthtext1.text!)-\(Daytext1.text!)")

    let islamic = NSCalendar(identifier: NSIslamicCalendar)

    let components = islamic?.components(NSCalendarUnit(rawValue: UInt.max), fromDate: GregorianDate!)

    resultlable.text = "\(components!.year) - \(components!.month) - \(components!.day)"

For Example , the user will enter a gregorian date (2015/09/29) and it will show the Hijri year (1436/12/15) .

but the result in this code is wrong .

SO , Please help .

Thanks a lot for x4h1d (https://stackoverflow.com/users/1459192/x4h1d) , Grimxn (https://stackoverflow.com/users/3729417/grimxn) and for everyone for help .

Community
  • 1
  • 1
Hassan Joseph
  • 87
  • 2
  • 8
  • Don't just ask us to write code for you. What have you tried? Please post your work-in-progress Swift implementation. – JAL Sep 25 '15 at 14:43
  • 1
    I would set the locale to Gregorian, create a universal timestamp from the Gregorian input, set the locale to Hijri, then create Hijri output from the universal timestamp. – Yimin Rong Sep 25 '15 at 14:52
  • sorry guys but it is the first post for me . Thanks a lot – Hassan Joseph Sep 26 '15 at 23:47

1 Answers1

1

You should use NSDate and NSDateFormatter for date conversions.

This question might help you.

Apple's documentation also gives a lot of useful information.

Community
  • 1
  • 1
wolfrevo
  • 2,006
  • 2
  • 25
  • 32
  • Gregorian to Hijri (only) The right answer in this link :http://stackoverflow.com/questions/32893323/convert-gregorian-date-to-hijri-date – Hassan Joseph Oct 05 '15 at 17:44