Last active
August 29, 2015 14:28
-
-
Save andrey-str/3377a2f9f2415efe2ced to your computer and use it in GitHub Desktop.
Absolute time to relative time(e.g. 2 days ago)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QString dateDiff(const QString&origDate) { | |
NSDateFormatter* df = [$CE dateFormatter]; | |
NSDate *convertedDate = [df dateFromString:origDate.toNSString()]; | |
NSDate *todayDate = [NSDate date]; | |
double ti = [convertedDate timeIntervalSinceDate:todayDate]; | |
ti = ti * -1; | |
if (ti < 1) { | |
return origDate; | |
} | |
else if (ti < 60) { | |
return "less than a Minute ago"; | |
} | |
else if (ti < 3600) { | |
int diff = (int) round(ti / 60); | |
return QString("%1 Minutes ago").arg(diff); | |
} | |
else if (ti < 86400) { | |
int diff = (int) round(ti / 60 / 60); | |
return QString("%1 Hours ago").arg(diff); | |
} | |
else if (ti < 2629743) { | |
int diff = (int) round(ti / 60 / 60 / 24); | |
return QString("%1 Days ago").arg(diff); | |
} | |
else { | |
return origDate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment