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

本小妞迷上赌 提交于 2019-11-29 04:04:48

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

Get Hajri Date:

Swift :

 let dateFor = DateFormatter()

 var 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]];

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 ١-١١-١٤٣٤

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!