Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift/iOS:30日NSDATE组件的异常行为_Ios_Date_Swift_Nsdateformatter - Fatal编程技术网

Swift/iOS:30日NSDATE组件的异常行为

Swift/iOS:30日NSDATE组件的异常行为,ios,date,swift,nsdateformatter,Ios,Date,Swift,Nsdateformatter,我看到很多关于iOS日历的“奇怪行为”帖子,但这篇似乎不在里面,所以我将把它添加到这堆帖子中 我有一个类,其中包括计算两个日期之间的差异,并将它们显示为月、日和年 我需要回去用ObjC测试一下,但我认为这不会发生,因为这是我几年前用C编写的一个非常旧的应用程序的重写 现在,今天是6月30日,我在写单元测试,注意到日历对两个不同的日期给出了相同的计数 确切地说,它说2012年12月30日到2014年6月30日正好是一年零六个月 我还说2012年12月31日到2014年6月30日是一年零六个月。确切

我看到很多关于iOS日历的“奇怪行为”帖子,但这篇似乎不在里面,所以我将把它添加到这堆帖子中

我有一个类,其中包括计算两个日期之间的差异,并将它们显示为月、日和年

我需要回去用ObjC测试一下,但我认为这不会发生,因为这是我几年前用C编写的一个非常旧的应用程序的重写

现在,今天是6月30日,我在写单元测试,注意到日历对两个不同的日期给出了相同的计数

确切地说,它说2012年12月30日到2014年6月30日正好是一年零六个月

我还说2012年12月31日到2014年6月30日是一年零六个月。确切地应该是1年5个月30天(我相信)

在任何情况下,两个日期都不应创建相同的时间间隔

这是我正在使用的代码。我看不出我做错了什么。有什么线索吗

顺便说一句:不管什么原因,这个代码在操场上都不起作用。也许我对系统要求太高了

import Foundation

class Gonkulator
{
    var years:Int
    var months:Int
    var days:Int

    init ( inStartDate:NSDate, inNowDate:NSDate )
    {
        // The reason for all this wackiness, is we want to completely strip out the time element of each date. We want the days to be specified at noon.
        var fromString:String = NSDateFormatter.localizedStringFromDate ( inStartDate, dateStyle: NSDateFormatterStyle.ShortStyle, timeStyle: NSDateFormatterStyle.NoStyle )
        var toString:String = NSDateFormatter.localizedStringFromDate ( inNowDate, dateStyle: NSDateFormatterStyle.ShortStyle, timeStyle: NSDateFormatterStyle.NoStyle )

        var dateFormatter = NSDateFormatter()
        dateFormatter.timeStyle = .NoStyle
        dateFormatter.dateStyle = .ShortStyle

        // We have stripped out the time information, and each day is at noon.
        var startDate:NSDate = dateFormatter.dateFromString ( fromString ).dateByAddingTimeInterval ( 43200 )    // Make it Noon, Numbah One.
        var stopDate:NSDate = dateFormatter.dateFromString ( toString ).dateByAddingTimeInterval ( 43200 )

        // Get the Gregorian calendar
        let myCalendar: NSCalendar = NSCalendar ( calendarIdentifier:NSGregorianCalendar )

        // Create our answer from the components of the result.
        var components = myCalendar.components ( NSCalendarUnit.YearCalendarUnit | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit, fromDate: startDate, toDate: stopDate, options: nil )
        self.years = components.year
        self.months = components.month
        self.days = components.day

//        DEBUG DISPLAY
//        fromString = NSDateFormatter.localizedStringFromDate ( startDate, dateStyle: NSDateFormatterStyle.ShortStyle, timeStyle: NSDateFormatterStyle.NoStyle )
//        toString = NSDateFormatter.localizedStringFromDate ( stopDate, dateStyle: NSDateFormatterStyle.ShortStyle, timeStyle: NSDateFormatterStyle.NoStyle )
//        println ( "Start Date: " + fromString + " -Years: \(self.years), Months: \(self.months), Days: \(self.days)" )
//        println ( "End Date: " + toString )
    }
}

var dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = .NoStyle
dateFormatter.dateStyle = .ShortStyle

var startDate:NSDate = dateFormatter.dateFromString ( "12/30/12" ).dateByAddingTimeInterval ( 43200 )    // Make it Noon, Numbah One.
var stopDate:NSDate = dateFormatter.dateFromString ( "06/30/14" ).dateByAddingTimeInterval ( 43200 )

// First create an instance for December 30, 18 months ago
let test = Gonkulator ( inStartDate: startDate, inNowDate: stopDate )
var years = test.years      // This will be 1
var months = test.months    // This will be 6
var days = test.days        // This will be 0

startDate = dateFormatter.dateFromString ( "12/31/12" ).dateByAddingTimeInterval ( 43200 )

// Next, create an instance for December 31
let test2 = Gonkulator ( inStartDate: startDate, inNowDate: stopDate )
years = test2.years      // This will be 1
months = test2.months    // This will be 6 #WhiskeyTangoFoxtrot
days = test2.days        // This will be 0 #WhiskeyTangoFoxtrot
第一次呼叫验证为:


第二个例子应该是

你的问题是你认为公历应该是合乎逻辑和统一的。例如,一个月到底有多长?你有一个观点(我们都应该使用阿兹特克日历,并在红衣主教的日期上进行祭祀仪式)。但是,请注意我今天早上刚刚添加的链接。他们给出了预期的结果。我确实将此作为雷达问题提交了文件,但我真的不喜欢责怪操作系统或工具(但事实上,这有点可能,因为很多东西还未成熟)。从1月底到4月底是3个月。从第1个月的30日到第4个月的30日也是3个月。在我看来,这似乎是合乎逻辑的——从一个荒谬的情况中,你能做出的最好的逻辑。对于这一点,没有单一的“正确”答案,因此人们必须选择一条似乎最不武断的规则并坚持下去,即使会产生一些明显的胡说八道。不同的设计人员可能会选择稍有不同的规则。请检查dateFormatString,您可能使用YY而不是YY,这可能会导致一些问题。