Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6573791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:10:41+00:00 2026-05-25T15:10:41+00:00

Possible Duplicate: iPhone: Convert date string to a relative time stamp I have a

  • 0

Possible Duplicate:
iPhone: Convert date string to a relative time stamp

I have a problem. I have tried creating a way to get a time since stamp such as “14 hours ago” but it doesn’t seem to be working. So I need to help to create a function that takes seconds from the time a database row/item was created. So if the db row was created 600 seconds ago that would mean the function would output “10 minutes ago”? I want it to return either seconds, minutes, hours, weeks, months or years. I know its probably really simple, I just can’t seem to get it right…

OH and this is for an iPhone app so it uses objective-c.

Any help would be much appreciated.

THIS IS WHAT I HAVE CURRENTLY:

-(NSString *)timeSinceTimestamp:(NSString *)seconds{
   double seconds2 = [seconds doubleValue];
   NSDate *date = [NSDate dateWithTimeIntervalSince1970:seconds2];
   NSDate *now = [NSDate date];
   double start = [date timeIntervalSince1970];
   double end = [now timeIntervalSince1970];
   double difference = (end - start) / 1000;
   difference = round(difference);
   int minutes = difference / 60;
   int hours = minutes / 60;
   int days = hours / 24;
   int weeks = days / 7;
   int months = weeks / 5;
   int years = months / 12;
   NSString *string;
   if(difference < 60){
        string = [NSString stringWithFormat:@"%i seconds ago",difference];
   }else if (minutes > 1 && minutes < 60) {
        string = [NSString stringWithFormat:@"%i minutes ago",minutes];
   }else if (hours > 1 && hours < 24) {
       string = [NSString stringWithFormat:@"%i hours ago",hours];
   }else if (days > 1 && days < 7) {
       string = [NSString stringWithFormat:@"%i days ago",days];
   }else if (weeks > 1 && weeks < 5) {
       string = [NSString stringWithFormat:@"%i weeks ago",weeks];
   }else if (months > 1  && months < 12) {
       string = [NSString stringWithFormat:@"%i months ago",months];
   }else if (years > 1 && years < 12) {
       string = [NSString stringWithFormat:@"%i years ago",years];
   }

     return string;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:10:43+00:00Added an answer on May 25, 2026 at 3:10 pm

    Calculate how many seconds your delta is. Use NSDate dateWithIntervalSince1970 or dateWithIntervalSinceReferenceDate to create an NSDate object from the seconds value. You can format that value into hours/minutes/seconds with NSDateFormatter. The month and year will be wrong but who cares?

    To calculate days and weeks the simplest thing to do is to divide the seconds by 24 * 60 * 60 to get days, then divide that by 7 to get weeks. Months and years are a problem since they are irregular, so you’d probably be better off to use calendrical calculations if you want that level of resolution. If you know that something was created 10000 seconds ago you can use NSDate dateWithTimeIntervalSinceNow (with a negative interval) to produce the date object for that earlier time.

    [By the way, your code above appears to be roughly correct (I didn’t go over it closely). I don’t see any reason why you couldn’t make it work, with a little effort.]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to Get time difference in iPhone I´m getting date and time
Possible Duplicate: Determine device (iPhone, iPod Touch) with iOS I have tried to get
Possible Duplicate: Text to speech on iPhone I have a string say Hello world.
Possible Duplicate: Real-time Pitch Shifting on the iPhone i have already found the answer
Possible Duplicate: Retrieving current local time on iPhone? How to get current time ?(hour:minute:second)
Possible Duplicate: Starting iPhone app development in Linux? Is there a way to use
Possible Duplicate: iPhone inputting NSUserDefaults into a UITextField Is there a way to remember
Possible Duplicate: How to programmatically send SMS on the iPhone? Does someone have an
Possible Duplicate: iPhone/iPad App Code Obfuscation - Is it Possible? Worth it? I have
Possible Duplicate: where can i download MGTwitterEngine for iphone sdk I have to integrate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.