Ios 比较不同日历中的两个日期组件

Ios 比较不同日历中的两个日期组件,ios,swift,nsdate,Ios,Swift,Nsdate,我想比较两个日期的“日”成分。其中一个日期是从包含波斯日历格式日期的JSON文件导入的。另一个是设备默认NSDate() 两个日期都指向同一天,但其中一个返回的日期组件与另一个不同。当我记录日组件时,它们是不同的 let cal2 = Calendar(identifier: .persian) if cal2.dateComponents([.day,.month,.year], from: day as Date) == cal2.dateComponents([.day,.month,.y

我想比较两个日期的“日”成分。其中一个日期是从包含波斯日历格式日期的JSON文件导入的。另一个是设备默认NSDate()

两个日期都指向同一天,但其中一个返回的日期组件与另一个不同。当我记录日组件时,它们是不同的

let cal2 = Calendar(identifier: .persian)
if cal2.dateComponents([.day,.month,.year], from: day as Date) == cal2.dateComponents([.day,.month,.year], from: date as Date)
{
   t = true
}

其中一个日期是:1395-07-20(波斯历),另一个日期是2016-10-11。这两个日期都相等并指向同一天。

您可以使用
日期格式化程序
(请参阅)将
日期
值转换为相同的日历格式。之后,您可以对它们进行比较。

您可以使用
日期格式化程序
(请参阅)将
日期
值转换为相同的日历格式。然后,您可以比较它们。

在这种情况下,您的控制台中应该有“true”:

//create 2 Date (from Persian calendar and from Gregorian calendar) that point to same day.
let componentPersian = NSDateComponents()
componentPersian.day = 20
componentPersian.month = 07
componentPersian.year = 1395
let date1 = persianCalendar?.dateFromComponents(componentPersian)

let componentGregorian = NSDateComponents()
componentGregorian.day = 11
componentGregorian.month = 10
componentGregorian.year = 2016
let date2 = gregorian?.dateFromComponents(componentGregorian)

//get NSDateComponents of those 2 Date, USING SAME CALENDAR. I use Gregorian in my test.
let components1 = gregorian?.components([.Day, .Month, .Year], fromDate: date1!)
let components2 = gregorian?.components([.Day, .Month, .Year], fromDate: date2!)

//and compare the values.
if (components1?.day == components2?.day
    && components1?.month == components2?.month
    && components1?.year == components2?.year
    ) {
    NSLog("true") //you must see this log in the console.
} else {
    NSLog("false")
}

在这种情况下,控制台中应该有“true”:

//create 2 Date (from Persian calendar and from Gregorian calendar) that point to same day.
let componentPersian = NSDateComponents()
componentPersian.day = 20
componentPersian.month = 07
componentPersian.year = 1395
let date1 = persianCalendar?.dateFromComponents(componentPersian)

let componentGregorian = NSDateComponents()
componentGregorian.day = 11
componentGregorian.month = 10
componentGregorian.year = 2016
let date2 = gregorian?.dateFromComponents(componentGregorian)

//get NSDateComponents of those 2 Date, USING SAME CALENDAR. I use Gregorian in my test.
let components1 = gregorian?.components([.Day, .Month, .Year], fromDate: date1!)
let components2 = gregorian?.components([.Day, .Month, .Year], fromDate: date2!)

//and compare the values.
if (components1?.day == components2?.day
    && components1?.month == components2?.month
    && components1?.year == components2?.year
    ) {
    NSLog("true") //you must see this log in the console.
} else {
    NSLog("false")
}

“NSDate”是一个绝对时间点,没有格式、日历或“day”属性。日期在JSON中的格式如何,如何“导入”它例如,您可以使用波斯日历将日期从JSON转换为NSDate,并使用默认日历将NSDate组件转换为NSDate组件。如果在同一个NSDate上使用两个不同的NSCALENDAR(我指的是格里高利历和波斯历),则没有相同的.day组件是正常的。@Hoa有解决方案吗?@MartinR我知道这一点。这就是我问这个问题的原因!这很奇怪,因为NSDate引用的是时间上的绝对值。您至少应该显示输入数据、迄今为止尝试的代码以及实际结果和预期结果。“NSDate”是绝对时间点,没有格式、日历或“day”属性。日期在JSON中的格式如何,如何“导入”它例如,您可以使用波斯日历将日期从JSON转换为NSDate,并使用默认日历将NSDate组件转换为NSDate组件。如果在同一个NSDate上使用两个不同的NSCALENDAR(我指的是格里高利历和波斯历),则没有相同的.day组件是正常的。@Hoa有解决方案吗?@MartinR我知道这一点。这就是我问这个问题的原因!这很奇怪,因为NSDates引用的是时间上的绝对值。您至少应该显示输入数据、迄今为止尝试的代码以及实际和预期的结果。没有帮助,仍然是一样的。你自己尝试过吗?在你的两个日期上调用NSLog(“date:(YOUR_date_VARIABLE)”),看看它们是否是同一天。实际上很奇怪,我从这段代码中得到的是一个转换为波斯日历格式字符串的日期!!!让m=DateFormatter()m.calendar=calendar(标识符:.gregorian)m.dateFormat=“yyyy mm dd”让str=m.string(from:day as Date)没有帮助,仍然是一样的。你自己尝试过吗?在你的两个日期上调用NSLog(“date:(YOUR_date_VARIABLE)”),看看它们是否是同一天。实际上很奇怪,我从这段代码中得到的是一个转换为波斯日历格式字符串的日期!!!设m=DateFormatter()m.calendar=calendar(标识符:.gregorian)m.dateFormat=“yyyy-mm-dd”设str=m.string(从:日期为日期)