在swift中两次之间的分钟

7gyucuyw  于 2023-09-29  发布在  Swift
关注(0)|答案(8)|浏览(145)

我找到一串“14:05”
我如何将它与当前时间进行比较,并以分钟为单位计算时差?
差值可以是负数和正数(当前时间在字符串之前或之后)

oiopk7p5

oiopk7p51#

Calendar有强大的方法来做到这一点。
完全不需要任何关于603600甚至86400的显式数学运算。

  • 将时间字符串转换为Date
  • 从当前日期和转换后的日期中获取hourminute日期组件。
  • 通过仅指定minute分量来计算与dateComponents(:from:to:的差值。
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!
eoxn13cs

eoxn13cs2#

为了得到两个日期之间的分钟差异,我做了下面的事情。
您可以将此方法保留在公共类和调用中。

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
   }
velaa5lx

velaa5lx3#

尽管有各种各样的答案,但我发现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
x7rlezfr

x7rlezfr4#

Objective-C版本

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;

Swift版本

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

    let date1 = dateFormatter.date(from: "14:05")

    let date2 = NSDate()

    var components1 = NSCalendar.current.dateComponents([.hour, .minute], from: date1!)

    let components2 = NSCalendar.current.dateComponents([.hour, .minute, .day, .month, .year], from: date2 as Date)

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

    let date3 = NSCalendar.current.date(from: components1)

    let timeIntervalInMinutes = date3!.timeIntervalSince(date2 as Date)/60
5vf7fwbs

5vf7fwbs5#

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

guicsvcw6#

请看下面@Alexey Kudley的答案的Swift实现。请给他一些爱的原始解决方案。

let inputString = "07:22"

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

guard let inputDate = formatter.date(from: inputString) else { return }
let nowDate = Date()

var inputComps = Calendar.current.dateComponents([.hour, .minute], from: inputDate)
let nowComps = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: nowDate)

inputComps.year = nowComps.year
inputComps.month = nowComps.month
inputComps.day = nowComps.day

guard let normalisedInputDate = Calendar.current.date(from: inputComps) else { return }

let intervalInMins = nowDate.timeIntervalSince(normalisedInputDate) / 60
vzgqcmou

vzgqcmou7#

你可以这样得到:

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)
}
z6psavjg

z6psavjg8#

func dateComponents(_ components:设置<Calendar.Component>,从开始:结束日期:Date)-> DateComponents
返回两个日期之间的差值。
let date1 = Date()
let date2 = Date()
let difference = Calendar.current.dateComponents([.minute],from:date1,to:date2).minute

相关问题