iOS is often used to show code snippets a few hours ago/days ago/months ago/years ago

Source: Internet
Author: User
Tags vars

iOS is often used to show code snippets a few hours ago/days ago/months ago/years ago2015-03-13 10:09
copyright Notice: Please pay attention to personal blog: http://www.henishuo.com/
print?
  1. /**
  2. * Retain a formated string with a real date string
  3. *
  4. * @param datestring a real date string, which can be converted to a NSDate object
  5. *
  6. * @return A string that'll be x minutes ago/x hours ago/yesterday/X days ago/x months ago/x years ago
  7. */
  8. + (NSString *) timeinfowithdatestring: (NSString *) datestring {
  9. //Format the date string as a Date object
  10. nsdate *date = [nsdate datefromstring:datestring withformat:@ "Yyyy-mm-dd HH:mm:ss"];
  11. nsdate *curdate = [nsdate Date];
  12. Nstimeinterval time =-[date timeintervalsincedate:curdate];
  13. int month = (int) ([curdate getMonth]-[date getMonth]);
  14. int year = (int) ([curdate getYear]-[date getYear]);
  15. int day = (int) ([curdate GetDay]-[date GetDay]);
  16. Nstimeinterval rettime = 1. 0;
  17. //Less than one hour
  18. if (Time < 3600) {
  19. Rettime = time/ 60;
  20. Rettime = Rettime <= 0. 0?  1. 0:rettime;
  21. return [NSString stringWithFormat:@ "%.0f minutes ago", Rettime];
  22. }
  23. //Less than one day, i.e. today
  24. Else if (Time < 33600 * 24) {
  25. Rettime = time/ 3600;
  26. Rettime = Rettime <= 0. 0?  1. 0:rettime;
  27. return [NSString stringWithFormat:@ "%.0f hours ago", Rettime];
  28. }
  29. //Yesterday
  30. Else if (Time < 33600 * 2* 2) {
  31. return @ "Yesterday";
  32. }
  33. //The first condition is the same year, and the time interval is within one months
  34. //The second condition is an alternate year, for the next year, only last December with this year January this situation
  35. Else if ((ABS (year) = = 0 && abs (month) <= 1)
  36. || (year) = = 1 && [curdate getMonth] = = 1 && [date getMonth] = = 12)) { /c10>
  37. int retday = 0;
  38. //Same year
  39. if (year = = 0) {
  40. //Same month
  41. if (month = = 0) {
  42. Retday = day;
  43. }
  44. }
  45. if (retday <= 0) {
  46. //This is calculated by the maximum monthly value.
  47. //Gets the release date, the total number of days in the month
  48. int totaldays = [NSDate daysinmonth: (int) [Date GetMonth] year : (int) [date getYear]];
  49. //Current days + (the total number of days in the release date-month of the release date, which is equal to the number of days from today)
  50. Retday = (int) [Curdate GetDay] + (Totaldays-(int) [date GetDay]);
  51. if (retday >= totaldays) {
  52. return [NSString stringWithFormat:@ "%d months ago", (ABS) (MAX (Retday/ 31, 1))];
  53. }
  54. }
  55. return [NSString stringWithFormat:@ "%d days ago", (ABS) (Retday)];
  56. } Else {
  57. if (ABS (year) <= 1) {
  58. if (year = = 0) { //
  59. return [NSString stringWithFormat:@ "%d months ago", ABS (month)];
  60. }
  61. //difference of one year
  62. int month = (int) [curdate getMonth];
  63. int premonth = (int) [date getMonth];
  64. //Every other year, but the same month, as a full year to calculate
  65. if (month = = 12 && premonth = = 12) {
  66. return @ "1 years ago";
  67. }
  68. //Nor look, but not the same month
  69. return [NSString stringWithFormat:@ "%d months ago", (ABS) (12-premonth + month)];
  70. }
  71. return [NSString stringWithFormat:@ "%d years ago", ABS (year)];
  72. }
  73. return @ "1 hours ago";
  74. }

The number of months before this calculation, in order to reduce the amount of calculation, not to get the total number of days for the corresponding month, but the month maximum value of 31 as the standard, therefore,

If you need more accurate calculations, replace the corresponding small piece of code

iOS is often used to show code snippets a few hours ago/days ago/months ago/years ago

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.