13

can someone help me please

I'm trying to create an IOS app using Swift language and I need to use Hijri (islamic) calendar

I tried many time but I failed :(

this is my try

    let datenow = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components(NSCalendarUnit(UInt.max), fromDate: datenow)

    var gregorian = NSCalendar(identifier:NSIslamicCivilCalendar)
    var date = gregorian.dateFromComponents(components)

    println(date)

and the output is wrong

   2576-04-25 09:05:08 +0000

We are in year 1434 in hijri not 2576 !

Abdulaziz Noor
  • 6,413
  • 2
  • 19
  • 21

5 Answers5

13

You're mixing up the calendars, dates & components:

let datenow = NSDate() 
    // This is a point in time, independent of calendars
let calendar = NSCalendar.currentCalendar() 
    // System calendar, likely Gregorian 
let components = calendar.components(NSCalendarUnit(UInt.max), fromDate: datenow) 
    // Gregorian components

println("\(components.year)") // "2014"

var islamic = NSCalendar(identifier:NSIslamicCivilCalendar)! // Changed the variable name 
    // *** Note also NSCalendar(identifier:) now returns now returns an optional ***
var date = islamic.dateFromComponents(components) 
    // so you have asked to initialise the date as AH 2014

println(date) 
    // This is a point in time again, sometime in AH 2014, or AD 2576

What you need to do is simply:

let datenow = NSDate()
let islamic = NSCalendar(identifier:NSIslamicCivilCalendar)!
let components = islamic.components(NSCalendarUnit(UInt.max), fromDate: datenow)
println("Date in system calendar:\(datenow), in Hijri:\(components.year)-\(components.month)-\(components.day)")
   //"Date in system calendar:2014-09-25 09:53:00 +0000, in Hijri:1435-11-30"

To get a formatted string, rather than just the integer components, you need to use NSDateFormatter, which will allow you to specify the calendar & date as well as the format. See here.

Update

To simply transliterate the numerals to (Eastern) Arabic numerals (as 0...9 are referred to as (Western) Arabic numerals to distinguish them from, say, Roman numerals), as requested, you could use:

let sWesternArabic = "\(components.day)-\(components.month)-\(components.year)"
let substituteEasternArabic = ["0":"٠", "1":"١", "2":"٢", "3":"٣", "4":"٤", "5":"٥", "6":"٦", "7":"٧", "8":"٨", "9":"٩"]
var sEasternArabic =  ""
for i in sWesternArabic {
    if let subs = substituteEasternArabic[String(i)] { // String(i) needed as i is a character
        sEasternArabic += subs
    } else {
        sEasternArabic += String(i)
    }
}

println("Western Arabic numerals = \(sWesternArabic), Eastern Arabic numerals = \(sEasternArabic)")
Grimxn
  • 22,115
  • 10
  • 72
  • 85
  • that work great but hot can I print the intger in arabic way like ٠٩/١٠/١٤٣٤ – Abdulaziz Noor Sep 25 '14 at 23:12
  • Printing the Arabic numerals is easy (month names harder) - just get it as a string using 1...9 and then use character substitution to replace 1 with Arabic 1, etc. – Grimxn Sep 26 '14 at 06:42
  • Added transliteration, as requested. Sorry to take all day, I've been working! – Grimxn Sep 26 '14 at 19:22
  • 1
    thank you Grimxn and I also find a great way to show arabic numbers let formatter = NSNumberFormatter() formatter.locale = NSLocale(localeIdentifier: "ar_SA") var date:String = "\(formatter.stringFromNumber(components.year))-\(formatter.stringFromNumber(components.month))-\(formatter.stringFromNumber(components.day))" it will print ١-١١-١٤٣٤ – Abdulaziz Noor Sep 27 '14 at 00:11
  • 1
    @AbdulazizNoor - That's really good - way better than my hack. You should post it as an answer, not a comment! – Grimxn Sep 27 '14 at 03:04
  • oh ,Im new here so I didn't know that I can ,but I just did ,thanks for advice :) – Abdulaziz Noor Sep 27 '14 at 03:10
  • I believe that placing the Arabic integers in the code files may raise a problem where the code file encoding on a specific developer machine/version control could manipulate the Arabic characters, so I think it's better to save them in a localizable strings file – Mina Wissa Mar 19 '17 at 09:41
7

Get Hajri Date:

Swift :

 let dateFor = DateFormatter()

 let hijriCalendar = Calendar.init(identifier: Calendar.Identifier.islamicCivil)
 dateFor.locale = Locale.init(identifier: "ar") // or "en" as you want to show numbers

 dateFor.calendar = hijriCalendar

 dateFor.dateFormat = "dd/MM/yyyy"
 print(dateFor.string(from: Date()))

Obj-C :

NSCalendar *hijriCalendar2 = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierIslamicCivil];
NSDateFormatter * lastDate = [[NSDateFormatter alloc]init];

lastDate.calendar = hijriCalendar2;
[lastDate setDateFormat:@"dd/MM/yyyy HH:mm:ss"];

NSString * dateString = [lastDate stringFromDate:[NSDate date]];
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
Marwan Alqadi
  • 795
  • 8
  • 14
  • Please add more description and/or information about your answer and how it solves the asked problem so others can easily understand it without asking for clarification – koceeng Mar 19 '17 at 10:03
4

thank you @Grimxn and I also find a great way to show arabic numbers

    let formatter = NSNumberFormatter()
    formatter.locale = NSLocale(localeIdentifier: "ar_SA")

    var date:String = "\(formatter.stringFromNumber(components.year))-\(formatter.stringFromNumber(components.month))-\(formatter.stringFromNumber(components.day))"

it will print ١-١١-١٤٣٤

Abdulaziz Noor
  • 6,413
  • 2
  • 19
  • 21
4

Swift 3 & 4

let dateNow = DateFormatter()
let islamicCalendar = Calendar.init(identifier: Calendar.Identifier.islamicCivil)
dateNow.calendar = islamicCalendar
dateNow.locale = Locale.init(identifier: "ar_SA") // This is used to show numbers, day and month in arabic letters
dateNow.dateFormat = "EEE dd MMM yyyy"

print("\(dateNow.string(from: Date()))")

find out and place date format from: http://userguide.icu-project.org/formatparse/datetime

Soufiane ROCHDI
  • 1,543
  • 17
  • 24
4

Swift 5:

let hijriCalendar = Calendar(identifier: .islamicCivil)

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "ar")
formatter.calendar = hijriCalendar
formatter.dateFormat = "dd/MM/yyyy"

print(formatter.string(from: Date()))
shbedev
  • 1,875
  • 17
  • 28