[英]Date from string is not coming properly and two dates difference also using Swift 3?
I have a date in string format, example:- "2017-07-31" or can be multiple dates (any) in string format. 我有一个字符串格式的日期,例如:-“ 2017-07-31”,或者可以是多个日期(任何)字符串格式。 My requirement is to check this date to current date and if it is greater than 0 and less than 15, then that time I have to do another operation.
我的要求是将此日期检查为当前日期,如果该日期大于0且小于15,则必须再次执行该操作。
So first I am converting that date string to in date format. 所以首先我将日期字符串转换为日期格式。 But it is giving one day ago date.
但这是一天前的日期。 Here is my code:
这是我的代码:
//Date from string
func dateFromString(date : String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let currentDate = (dateFormatter.date(from: date))//(from: date))
return currentDate!
}
Ex. 防爆。 my date is "2017-08-30" and this function is returning 2017-08-29 18:30:00 +0000 in date format.
我的日期是“ 2017-08-30”,此函数以日期格式返回2017-08-29 18:30:00 +0000。 It means 1 day ago.
这意味着1天前。 I am little bit confuse about dates operation.
我对日期操作有些困惑。 I read so many blogs also.
我也读了很多博客。
After that I have to check this date to current date if it is in between 0 < 15 than I will do other operation. 之后,我必须检查该日期到当前日期(如果它在0 <15之间),那么我将执行其他操作。
Comparing two dates: 比较两个日期:
extension Date {
func daysBetweenDate(toDate: Date) -> Int {
let components = Calendar.current.dateComponents([.day], from: self, to: toDate)
return components.day ?? 0
}
}
If my date is today date and comparing to tomorrow date then also it is giving 0 days difference. 如果我的日期是今天的日期,并且与明天的日期进行比较,那么它也相差0天。 Why?
为什么?
If – for example – the current date is 2017-07-31 at 11AM then the difference to 2017-08-01 (midnight) is 0 days and 13 hours, and that's why you get "0 days difference" as result. 例如,如果当前日期是2017年7月31日上午11点,那么与2017年8月1日(午夜)的时差是0天13小时,这就是为什么您得到“ 0天时差”的原因。
What you probably want is to compare the difference between the start of the current day and the other date in days: 您可能想要的是比较当日开始日期与其他日期(以天为单位)之间的差异:
extension Date {
func daysBetween(toDate: Date) -> Int {
let cal = Calendar.current
let startOfToday = cal.startOfDay(for: self)
let startOfOtherDay = cal.startOfDay(for: toDate)
return cal.dateComponents([.day], from: startOfToday, to: startOfOtherDay).day!
}
}
Try this method for convert string to date: 尝试使用以下方法将字符串转换为日期:
func dateFromString(date : String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC")
let currentDate = (dateFormatter.date(from: date))//(from: date))
return currentDate!
}
Try this to compare the time between two dates in seconds : 尝试比较两个日期之间的时间(以秒为单位):
var seconds = Calendar.current.dateComponents([.second], from: date1!, to: date2!).second ?? 0
seconds = abs(seconds)
let min = seconds/60 // this gives you the number of minutes between two dates
let hours = seconds/3600 // this gives you the number of hours between two dates
let days = seconds/3600*24 // this gives you the number of days between two dates
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.