时差计算Android和iOS

时间:2016-05-15 15:56:32

标签: android ios swift datediff

我有一个在IIS上运行的网站,它将采用两个时间变量,计算这些变量并在-Minutes中产生时间差异(例如StartTime =" 18:00",EndTime =" 18:30",Diff =" -30")

我使用DateDiff()函数在VBScript中完美地工作。我已经搜索过高低,为Android和iOS寻找类似的解决方案,这样我就可以将时间计算建立到我的移动应用程序中,并让它可以脱机使用(例如,如果没有互联网连接)。

这是我在VBScript中使用的代码的一部分:

   //Grab the values from the form controls


    occtime = occDate.Value //from form controls
    testTime = testDate.Value //from form controls

    //work out the time difference in minutes. Should always a negative number.
    timeCalc = DateDiff("n", testTime, occtime)

使用时间变量,HH:MM或使用完整日期dd / mm / yyyy HH:MM会给我我需要的结果,但是我很难在iOS(Swift)中使用它或Android Studio。

先谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

对于iOS,请在XCode playgorund中尝试使用此Swift代码:

import Foundation

let curerntDate = NSDate()
let futureDate = NSDate(timeInterval: 36*60*60, sinceDate: curerntDate)

let currentCalendar = NSCalendar.currentCalendar()
let differenceDateComponents = currentCalendar.components([.Era, .Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: curerntDate, toDate: futureDate, options: NSCalendarOptions(rawValue: 0))
print(differenceDateComponents)

答案 1 :(得分:0)

Swift 2.2兼容扩展程序:

extension NSDate {
        func yearsFrom(date:NSDate) -> Int{
            return NSCalendar.currentCalendar().components(.Year, fromDate: date, toDate: self, options: []).year
        }
        func monthsFrom(date:NSDate) -> Int{
            return NSCalendar.currentCalendar().components(.Month, fromDate: date, toDate: self, options: []).month
        }
        func weeksFrom(date:NSDate) -> Int{
            return NSCalendar.currentCalendar().components(.WeekOfYear, fromDate: date, toDate: self, options: []).weekOfYear
        }
        func daysFrom(date:NSDate) -> Int{
            return NSCalendar.currentCalendar().components(.Day, fromDate: date, toDate: self, options: []).day
        }
        func hoursFrom(date:NSDate) -> Int{
            return NSCalendar.currentCalendar().components(.Hour, fromDate: date, toDate: self, options: []).hour
        }
        func minutesFrom(date:NSDate) -> Int{
            return NSCalendar.currentCalendar().components(.Minute, fromDate: date, toDate: self, options: []).minute
        }
        func secondsFrom(date:NSDate) -> Int{
            return NSCalendar.currentCalendar().components(.Second, fromDate: date, toDate: self, options: []).second
        }
        func offsetFrom(date:NSDate) -> String {
            if yearsFrom(date)   > 0 { return "\(yearsFrom(date))y"   }
            if monthsFrom(date)  > 0 { return "\(monthsFrom(date))M"  }
            if weeksFrom(date)   > 0 { return "\(weeksFrom(date))w"   }
            if daysFrom(date)    > 0 { return "\(daysFrom(date))d"    }
            if hoursFrom(date)   > 0 { return "\(hoursFrom(date))h"   }
            if minutesFrom(date) > 0 { return "\(minutesFrom(date))m" }
            if secondsFrom(date) > 0 { return "\(secondsFrom(date))s" }
            return ""
        }
    }

对于Android,这些方法运作良好:

public static int calcDaysDiff(Date day1, Date day2) {
        Date d1 = new Date(day1.getTime());
        Date d2 = new Date(day2.getTime());
        Calendar date1 = Calendar.getInstance();
        date1.setTime(d1);
        Calendar dateCpy = (Calendar) date1.clone();
        Calendar date2 = Calendar.getInstance();
        date2.setTime(d2);
        //checks if the start date is later then the end date - gives 0 if it is
        if (date1.get(Calendar.YEAR) >= date2.get(Calendar.YEAR)) {
        if (date1.get(Calendar.DAY_OF_YEAR) >= date2.get(Calendar.DAY_OF_YEAR)) {
            return 0;
        }
        }
        //checks if there is a daylight saving change between the two dates
        boolean isDate1Summer = TimeZone.getDefault().inDaylightTime(d1);
        boolean isDate2Summer = TimeZone.getDefault().inDaylightTime(d2);
        int offset = 0;
        //check if there as been a change in winter/summer time and adds/reduces an hour
        if (isDate1Summer && !isDate2Summer) {
            offset = 1;
        }
        if (!isDate1Summer && isDate2Summer) {
            offset = -1;
        }
        return calcDaysDiffAux(dateCpy, date2) + checkFullDay(dateCpy, date2, offset);
    }

    // check if there is a 24 hour diff between the 2 dates including the daylight saving offset
    public static int checkFullDay(Calendar day1, Calendar day2, int offset) {
        if (day1.get(Calendar.HOUR_OF_DAY) <= day2.get(Calendar.HOUR_OF_DAY) + offset) {
            return 0;
        }
        return -1;
    }

    // find the number of days between the 2 dates. check only the dates and not the hours
    public static int calcDaysDiffAux(Calendar day1, Calendar day2) {
        Calendar dayOne = (Calendar) day1.clone(),
                dayTwo = (Calendar) day2.clone();

        if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) {
            return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR));
        } else {
            if (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) {
                //swap them
                Calendar temp = dayOne;
                dayOne = dayTwo;
                dayTwo = temp;
            }
            int extraDays = 0;

            while (dayOne.get(Calendar.YEAR) > dayTwo.get(Calendar.YEAR)) {
                dayOne.add(Calendar.YEAR, -1);
                // getActualMaximum() important for leap years
                extraDays += dayOne.getActualMaximum(Calendar.DAY_OF_YEAR);
            }
            return extraDays - dayTwo.get(Calendar.DAY_OF_YEAR) + dayOne.get(Calendar.DAY_OF_YEAR);
        }
    }