Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

ios - Hijri (islamic) calendar in swift ??????? ??????

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 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...