Swift - 检查时间戳是昨天,今天,明天还是X天前 [英] Swift - check if a timestamp is yesterday, today, tomorrow, or X days ago
问题描述
我正在尝试弄清楚如何确定今天是否发生给定时间戳,或+1 / -1天。基本上,我想做这样的事情(伪代码)
I'm trying to work out how to decide if a given timestamp occurs today, or +1 / -1 days. Essentially, I'd like to do something like this (Pseudocode)
IF days_from_today(timestamp) == -1 RETURN 'Yesterday'
ELSE IF days_from_today(timestamp) == 0 RETURN 'Today'
ELSE IF days_from_today(timestamp) == 1 RETURN 'Tomorrow'
ELSE IF days_from_today(timestamp) < 1 RETURN days_from_today(timestamp) + ' days ago'
ELSE RETURN 'In ' + days_from_today(timestamp) + ' ago'
但至关重要的是,它需要在Swift中,我正在努力使用NSDate / NSCalendar对象。我开始计算这样的时差:
Crucially though, it needs to be in Swift and I'm struggling with the NSDate / NSCalendar objects. I started with working out the time difference like this:
let calendar = NSCalendar.currentCalendar()
let date = NSDate(timeIntervalSince1970: Double(timestamp))
let timeDifference = calendar.components([.Second,.Minute,.Day,.Hour],
fromDate: date, toDate: NSDate(), options: NSCalendarOptions())
然而,以这种方式比较并不容易,因为 .Day
根据一天中的时间和时间戳而有所不同。在PHP中,我只是使用mktime创建一个基于当天开始的新日期(即 mktime(0,0,0)
),但我是不确定在Swift中最简单的方法。
However comparing in this way isn't easy, because the .Day
is different depending on the time of day and the timestamp. In PHP I'd just use mktime to create a new date, based on the start of the day (i.e. mktime(0,0,0)
), but I'm not sure of the easiest way to do that in Swift.
有没有人对如何处理这个问题有个好主意?也许NSDate的扩展或类似的东西最好?
Does anybody have a good idea on how to approach this? Perhaps an extension to NSDate or something similar would be best?
推荐答案
NSCalendar
具有所有三种情况的方法
NSCalendar
has methods for all three cases
func isDateInYesterday(_ date: NSDate) -> Bool
func isDateInToday(_ date: NSDate) -> Bool
func isDateInTomorrow(_ date: NSDate) -> Bool
要计算早于昨天的天数,请使用
To calculate the days earlier than yesterday use
func components(_ unitFlags: NSCalendarUnit,
fromDate startingDate: NSDate,
toDate resultDate: NSDate,
options opts: NSCalendarOptions) -> NSDateComponents
将 .Day
传递给 unitFlags
并从结果中获取天
属性。
pass .Day
to unitFlags
and get the day
property from the result.
编辑:
这是一个函数,它通过剥离时间部分来考虑在
中的早期和晚期日期。
This is a function which considers also is in
for earlier and later dates by stripping the time part.
func dayDifferenceFromDate(timestamp : Int) -> String
{
let calendar = NSCalendar.currentCalendar()
let date = NSDate(timeIntervalSince1970: Double(timestamp))
if calendar.isDateInYesterday(date) { return "Yesterday" }
else if calendar.isDateInToday(date) { return "Today" }
else if calendar.isDateInTomorrow(date) { return "Tomorrow" }
else {
let startOfNow = calendar.startOfDayForDate(NSDate())
let startOfTimeStamp = calendar.startOfDayForDate(date)
let components = calendar.components(.Day, fromDate: startOfNow, toDate: startOfTimeStamp, options: [])
let day = components.day
if day < 1 { return "\(abs(day)) days ago" }
else { return "In \(day) days" }
}
}
Swift 3 +的更新:
func dayDifference(from interval : TimeInterval) -> String
{
let calendar = NSCalendar.current
let date = Date(timeIntervalSince1970: interval)
if calendar.isDateInYesterday(date) { return "Yesterday" }
else if calendar.isDateInToday(date) { return "Today" }
else if calendar.isDateInTomorrow(date) { return "Tomorrow" }
else {
let startOfNow = calendar.startOfDay(for: Date())
let startOfTimeStamp = calendar.startOfDay(for: date)
let components = calendar.dateComponents([.day], from: startOfNow, to: startOfTimeStamp)
let day = components.day!
if day < 1 { return "\(abs(day)) days ago" }
else { return "In \(day) days" }
}
}
这篇关于Swift - 检查时间戳是昨天,今天,明天还是X天前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!