快速进行日期转换和日期差异问题 [英] Date Conversion and date difference issue in swift

查看:66
本文介绍了快速进行日期转换和日期差异问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个应用程序中,我有从服务器获取多个数据的日期和时间。我已经在表视图中显示了这一点。现在,我必须根据上一行和下一行的日期和时间差,根据某些条件找到特定行的空闲时间。诸如车辆的出发和到达以及之间的空闲时间之类的东西。我将向您显示发生问题的已发出代码(仅针对我两个都有有效日期的情况)。

In one of my app I have date and time for multiple data fetching from server. I have shown this in tableview. Now from this I have to find some idle time for particular row by some condition by previous and next row's date and time difference. Something like departure and arrival of vehicle and idle time in between. I will show you the issued code where the issue occurs (only for I have both valid date).

let dFormater = NSDateFormatter()
dFormater.dateFormat = "ddMMMYYHHmm"
dFormater.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dFormater.timeZone = NSTimeZone(name: "GMT")
dFormater.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)

print(startDate)
print(endDate)

if let validFdate : NSDate = dFormater.dateFromString(startDate) {

    if let validLdate : NSDate = dFormater.dateFromString(endDate) {

        print(validFdate)
        print(validLdate)

        let minutes = validLdate.minutesFrom(validFdate)

        print("LO P & N \(indexPath.row)= \(minutes)")

        let hour = minutes/60
        let min = minutes%60

        let fullTime = NSString.init(format: "%02d:%02d", Int(hour),Int(min))
        strToReturn = fullTime as String

    } 
}

并为此登录就像

03JUN161411
04JUN160542
2015-12-20 08:41:00 +0000
2015-12-20 00:12:00 +0000
LO P & N 0= -509.0
04JUN160931
05JUN160506
2015-12-20 04:01:00 +0000
2015-12-19 23:36:00 +0000
LO P & N 0= -265.0
07JUN160530
07JUN162127
2015-12-20 00:00:00 +0000
2015-12-20 15:57:00 +0000
LO P & N 2= 957.0
08JUN160049
08JUN161616
2015-12-19 19:19:00 +0000
2015-12-20 10:46:00 +0000
LO P & N 1= 927.0

现在从此记录的输出中,您可以看到认为第一个有效日期两个仍然显示错误的输出,因为有不同的日期,而在最后两个输出中有完美的输出,因为两个日期是相同的日期。以及为什么它更改日期月份和年份。请参见

Now From this logged output, you can see that thought there is valid date in first two still it show wrong output as there are different date and in last two output there is perfect output as both date are of same date. And also why it changes date month and year. See

03JUN161411

2015-12-20 08:41:00 +0000

(我仅针对日期此处

推荐答案

您的日期格式不正确。

dFormater.dateFormat = "ddMMMYYHHmm"

资本Y是基于每年的日历中的年份。您需要使用小写字母Y:

Capital Y is the year in week-of-year based calendars. You need to use lower case Y:

dFormater.dateFormat = "ddMMMyyHHmm"

这篇关于快速进行日期转换和日期差异问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆