快速两次分钟

时间:2018-01-23 11:01:07

标签: ios swift

我收到了一个字符串“14:05”

我如何将它与当前时间进行比较,并在几分钟内得到时差?

差异可能是消极的和积极的(当前时间在字符串之前或之后)

7 个答案:

答案 0 :(得分:7)

Calendar有强大的方法来做到这一点。

根本不需要603600甚至86400的明确数学。

  • 将时间字符串转换为Date
  • 从当前和转换日期获取hourminute日期组件。
  • 通过仅指定dateComponents(:from:to:组件来计算与minute的差异。
let time = "14:05"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let timeDate = dateFormatter.date(from: time)!
let calendar = Calendar.current
let timeComponents = calendar.dateComponents([.hour, .minute], from: timeDate)
let nowComponents = calendar.dateComponents([.hour, .minute], from: Date())

let difference = calendar.dateComponents([.minute], from: timeComponents, to: nowComponents).minute!

答案 1 :(得分:3)

Objective-C版

Setup Complete!
AT
OK
AT+CENG?
+CENG: 0,0

OK
AT+CNETSCAN
Operator:"405803",MCC:405,MNC:803,Rxlev:62,Cellid:51A5,Arfcn:702
Operator:"405803",MCC:405,MNC:803,Rxlev:51,Cellid:51A7,Arfcn:709
Operator:"405803",MCC:405,MNC:803,Rxlev:45,Cellid:35D6,Arfcn:703
Operator:"405803",MCC:405,MNC:803,Rxlev:37,Cellid:51A6,Arfcn:707
Operator:"Airtel",MCC:404,MNC:45,Rxlev:30,Cellid:08EC,Arfcn:53
Operator:"Airtel",MCC:404,MNC:45,Rxlev:28,Cellid:08E9,Arfcn:55
Operator:"Airtel",MCC:404,MNC:45,Rxlev:25,Cellid:08EA,Arfcn:103
Operator:"Airtel",MCC:404,MNC:45,Rxlev:18,Cellid:AF79,Arfcn:59
Operator:"Airtel",MCC:404,MNC:45,Rxlev:22,Cellid:186B,Arfcn:60
Operator:"Airtel",MCC:404,MNC:45,Rxlev:30,Cellid:08EB,Arfcn:61
Operator:"Hutch-Kamataka",MCC:404,MNC:86,Rxlev:51,Cellid:11B5,Arfcn:725
Operator:"Hutch-Kamataka",MCC:404,MNC:86,Rxlev:51,Cellid:11B4,Arfcn:666
Operator:"Spice Telecom",MCC:404,MNC:44,Rxlev:37,Cellid:0A5D,Arfcn:22
Operator:"Spice Telecom",MCC:404,MNC:44,Rxlev:34,Cellid:0A5B,Arfcn:25
Operator:"Spice Telecom",MCC:404,MNC:44,Rxlev:33,Cellid:0A5C,Arfcn:17
Operator:"Bharat Karnataka",MCC:404,MNC:71,Rxlev:21,Cellid:DB93,Arfcn:76

Swift版本

    NSDateFormatter *formatter = [NSDateFormatter new];
    formatter.dateFormat = @"HH:mm";

    NSDate *date1 = [formatter dateFromString:@"14:15"];

    NSDate *date2 = [NSDate date];

    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date1];

    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitYear| NSCalendarUnitDay| NSCalendarUnitMonth fromDate:date2];

    components1.year = components2.year;
    components1.month = components2.month;
    components1.day = components2.day;

    NSDate *date3 = [[NSCalendar currentCalendar] dateFromComponents:components1];

    NSInteger timeIntervalInMinutes = [date3 timeIntervalSinceDate:date2]/60;

答案 2 :(得分:3)

尽管有所有答案,但我发现Narashima的答案最为直接。但是,这里有一个更简单的方法。

let startDate = Date()
let endDate = Date(timeInterval: 86400, since: startDate)

let diffSeconds = Int(endDate.timeIntervalSince1970 - startDate.timeIntervalSince1970)

let minutes = diffSeconds / 60
let hours = diffSeconds / 3600

答案 3 :(得分:0)

var dateParse = ""

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.locale = Locale(identifier: "fr_FR")

let dateString = dateFormatter.string(from: Date())
let dateNSDate = dateFormatter.date(from: date) //date = "14:05"
let currentDate = dateFormatter.date(from: dateString)
let timeInterval = currentDate.timeIntervalSince(dateNSDate!)

timeInterval以秒为单位。

如果您想将秒数转换为天,小时,分钟和秒:

print(String((seconds / 86400)) + " days")
print(String((seconds % 86400) / 3600) + " hours")
print(String((seconds % 3600) / 60) + " minutes")
print(String((seconds % 3600) % 60) + " seconds")

所以你的字符串是这样的:

let string = "\((seconds % 86400) / 3600)) : \((seconds % 3600) / 60))

答案 4 :(得分:0)

请参阅下面的@Alexey Kudley答案的Swift实现。请告诉他对原始解决方案的一些喜爱。

  std::function<bool(Args&)> sig {invokable_};

答案 5 :(得分:0)

你可以这样:

let receivedTimeString = "14:05"

let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"

if let date = formatter.date(from: receivedTimeString) {
    // getting the number of minutes in the receivedTimeString:
    let receivedTimeHoursMinutes = Calendar.current.component(.hour, from: date) * 60
    let receivedTimeMinutes = Calendar.current.component(.minute, from: date)
    let totalreceivedTimeMinutes = receivedTimeHoursMinutes + receivedTimeMinutes

    // getting the number of minutes in today:
    let todayHoursMinutes = Calendar.current.component(.hour, from: Date()) * 60
    let todayMinutes = Calendar.current.component(.minute, from: Date())
    let todayTimeMinutes = todayHoursMinutes + todayMinutes

    // difference in minutes (could be negative):
    let difference = totalreceivedTimeMinutes - todayTimeMinutes
    print(difference)
}

答案 6 :(得分:0)

为了获得两个日期之间的分钟差,我做了如下操作。

您可以将此方法保留在普通类中并调用。

func getMinutesDifferenceFromTwoDates(start: Date, end: Date) -> Int
   {

       let diff = Int(end.timeIntervalSince1970 - start.timeIntervalSince1970)

       let hours = diff / 3600
       let minutes = (diff - hours * 3600) / 60
       return minutes
   }