Search code examples
swiftnsdateios9datepickerdialog

How to customize the DatePickerDialog to show custom date in the text? - Swift


I am trying to change the date written inside the datePickerDialog to make Hijri Islamic date.

I found the function of showing the Hijri date as NSDateComponents

func hijriDate(dateToConvert: NSDate) -> NSDateComponents{
        let islamic = NSCalendar(identifier:NSCalendarIdentifierIslamicCivil)!
        let hijriComponents = islamic.components(NSCalendarUnit(rawValue: UInt.max), fromDate: dateToConvert)
        print("Date in system calendar:\(dateToConvert), in Hijri:\(hijriComponents.year)-\(hijriComponents.month)-\(hijriComponents.day)")
        return hijriComponents
        //"Date in system calendar:2014-09-25 09:53:00 +0000, in Hijri:1435-11-30"
}

So when the datePicker will print the date i wanna show the islamic component by converting the NSDate to Hijri Components.

So is there any way to show custom text for the datePickerDialog ? So I can use the function above to print out the date as Hijri from the NSDate ?

UPDATE:

The current DatePickerDialog I use is this:

let STR_DATE_FORMAT = "EEEE d, MMM yyyy"

DatePickerDialog().show("Select date".localized(), doneButtonTitle: "OK".localized(), cancelButtonTitle: "Cancel".localized(), datePickerMode: .Date, defaultDate:self.data.event_date ) {
                (date) -> Void in
                self.data.event_date = date
                self.event_date.text = date.toString(format: .Custom(STR_DATE_FORMAT)) as String
}

Solution

  • Set calendar of the date picker as below,

      datePicker.calendar = NSCalendar(identifier:NSCalendarIdentifierIslamicCivil)!
    

    Result looks like,

    enter image description here