How to set visited time like 1 min Ago etc.
func getDateAndTimeFormated(strDate : String)-> String {
let isoDate = strDate
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.date(from: isoDate)
print("my custom dateFormater :- \(date)")
let splitdateAndTime = strDate.split(separator: " ")
let splitDate = splitdateAndTime[0].split(separator: "-")
let splitTime = splitdateAndTime[1].split(separator: ":")
let year = Int(splitDate[0])
let month = Int(splitDate[1])
let days = Int(splitDate[2])
let hours = Int(splitTime[0])
let miniute = Int(splitTime[1])
let second = Int(splitTime[2])
var now = Date()
var nowComponents = DateComponents()
let calendar = Calendar.current
nowComponents.year = Calendar.current.component(.year, from: now)
nowComponents.month = Calendar.current.component(.month, from: now)
nowComponents.day = Calendar.current.component(.day, from: now)
nowComponents.hour = Calendar.current.component(.hour, from: now)
nowComponents.minute = Calendar.current.component(.minute, from: now)
nowComponents.second = Calendar.current.component(.second, from: now)
nowComponents.timeZone = NSTimeZone.local
let myYear = nowComponents.year! - year!
let myMonth = nowComponents.month! - month!
let myDays = nowComponents.day! - days!
let myHours = nowComponents.hour! - hours!
let myMiniute = nowComponents.minute! - miniute!
let mySecond = nowComponents.second! - second!
now = calendar.date(from: nowComponents)!
if myYear >= 0
{
if myYear == 1
{
return "\(myMonth) \("str_year_ago".localized)"
}
else{
return "\(myMonth) \("str_years_ago".localized)"
}
}
if myMonth >= 0 && myYear == 0
{
if myMonth == 1
{
return "\(myMonth) \("str_month_ago".localized)"
}
else{
return "\(myMonth) \("str_months_ago".localized)"
}
}
if myDays >= 1 && myYear < 1 && myMonth < 1
{
if myDays == 1
{
return "\(myDays) \("str_day_ago".localized)"
}
else{
return "\(myDays) \("str_days_ago".localized)"
}
}
if myHours >= 1 && myHours <= 24 && myDays < 1 && myHours > 0 && myYear < 1
{
if myHours == 1
{
return "\(myHours) \("str_hour_ago".localized)"
}
else{
return "\(myHours) \("str_hours_ago".localized)"
}
}
if myMiniute >= 1 && myMiniute <= 60 && myDays < 1 && myHours < 1 && myYear < 1 && myDays < 1
{
if myMiniute == 1
{
return "\(myMiniute) \("str_minute_ago".localized))"
}
else{
return "\(myMiniute) \("str_minutes_ago".localized))"
}
}
if mySecond <= 60 && mySecond == 0 && myMiniute == 0 && myHours == 0 && myDays == 0 && myYear < 1
{
return "\("str_just_now".localized)"
}
if mySecond <= 15 && myMiniute == 0 && myHours == 0 && myDays == 0 && myYear < 1
{
return "Just now"
}
return strDate
}
Comments
Post a Comment