gpt4 book ai didi

ios - 快速回历(伊斯兰)日历

转载 作者:IT王子 更新时间:2023-10-29 05:39:40 34 4
gpt4 key购买 nike

谁能帮帮我

我正在尝试使用 Swift 语言创建一个 IOS 应用程序,我需要使用回历(伊斯兰)日历

我试了很多次都失败了:(

这是我的尝试

    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)

输出错误

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

我们在回历 1434 年而不是 2576 年!

最佳答案

您混淆了日历、日期和组件:

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

你需要做的很简单:

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"

要获得格式化的字符串,而不仅仅是整数部分,您需要使用 NSDateFormatter,这将允许您指定日历和日期以及格式。参见 here .

更新

简单地将数字音译为(Eastern) Arabic numerals (因为 0...9 被称为(西方)阿拉伯数字以区别于罗马数字),根据要求,您可以使用:

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

关于ios - 快速回历(伊斯兰)日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26034840/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com