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 4598434
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:23:54+00:00 2026-05-21T23:23:54+00:00

I use the below Utility method to display the seconds, minutes, hours, etc gone

  • 0

I use the below Utility method to display the seconds, minutes, hours, etc gone by.

e.g., 1 hour ago

I have a friend overseas testing our iPhone application and every time they submit an item the method always returns “1 hour ago” because New Zealand is 2 hours ahead and the server is hosted in Australia, which is obviously 2 hours behind.

It’s only until our SQL Server catches up; when the item date starts to calculate properly.

Just wondering how I can handle time zone differences and modify my Utility method to work globally? Please feel free to help modify the method so it’s not so many lines of code (if you’re feeling really bored – and generous).

Thanks in advance.

+ (NSString *)elapsedTime:(NSDate *)date {
    NSUInteger desiredComponents = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

    NSDateComponents *elapsedTimeUnits = [[NSCalendar currentCalendar] components:desiredComponents
                                                                         fromDate:date
                                                                           toDate:[NSDate date]
                                                                          options:0];

    // Format to be used to generate string to display.
    NSString *scannedFormat = @"%d %@ ago";
    NSInteger number = 0;
    NSString *unit;

    if ([elapsedTimeUnits year] > 0) {
        number = [elapsedTimeUnits year];
        unit = [NSString stringWithFormat:@"year"];
    } else if ([elapsedTimeUnits month] > 0) {
        number = [elapsedTimeUnits month];
        unit = [NSString stringWithFormat:@"month"];
    } else if ([elapsedTimeUnits week] > 0) {
        number = [elapsedTimeUnits week];
        unit = [NSString stringWithFormat:@"week"];
    } else if ([elapsedTimeUnits day] > 0) {
        number = [elapsedTimeUnits day];
        unit = [NSString stringWithFormat:@"day"];
    } else if ([elapsedTimeUnits hour] > 0) {
        number = [elapsedTimeUnits hour];
        unit = [NSString stringWithFormat:@"hour"];
    } else if ([elapsedTimeUnits minute] > 0) {
        number = [elapsedTimeUnits minute];
        unit = [NSString stringWithFormat:@"minute"];
    } else if ([elapsedTimeUnits second] > 0) {
        number = [elapsedTimeUnits second];
        unit = [NSString stringWithFormat:@"second"];
    } else {
        number = 1;
        unit = @"second";
    }

    // Check if unit number is greater than 1, then append 's' at the end.
    if (number > 1) {
        unit = [NSString stringWithFormat:@"%@s", unit];
    }

    // Resultant string required.
    NSString *scannedString = [NSString stringWithFormat:scannedFormat, number, unit];

    return scannedString;
}
  • 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-21T23:23:55+00:00Added an answer on May 21, 2026 at 11:23 pm

    You’ll need to use [NSTimeZone localTimeZone] to get the time zone the iPhone is currently in, and then you’ll need to handle the difference between local time and server time. The safest option is probably to convert all times to GMT internally (especially when submitting times to the server), and then convert to local time when displaying time to the user.

    You could also use “server time” as a reference time zone, make sure your app knows (either by querying the server or hard-wiring it into your app) what that time zone is, and then convert all time values to “server time” internally… but that could run into problems if your server changes time zones, or off-by-an-hour issues when your server starts and ends Daylight Savings Time.

    You could also store time zones as part of the data you’re writing to the server, but really that’s just putting off the inevitable: Sooner or later you’ll need to convert these times to a reference time zone, so that you can calculate elapsed time.

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

Sidebar

Related Questions

I have a few places where I use the line below to clear out
I have one text input and one button (see below). How can I use
I typically use the below function to return the root URL if I ever
In the code below I use mpf_add to add the string representation of two
When I try to use the code below I get a duplicate variable error
I'm trying to use the code below to send messages via System.Net.Mail and am
When I run a Test Project on Visual Studio I use the code below
Below is my stored procedure. I want use stored procedure select all row of
Considering the criteria listed below, which of Python, Groovy or Ruby would you use?
Some batch files on Windows use an IF syntax with multiple lines, as below:

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.