自创建以来的时间(以秒为单位)转换为可读时间 - Objective-C

发布于 2024-12-04 18:49:00 字数 1917 浏览 0 评论 0原文

可能的重复:
iPhone:将日期字符串转换为相对时间戳

我有一个问题。我尝试创建一种方法来获取自时间戳以来的时间,例如“14 小时前”,但它似乎不起作用。因此,我需要帮助创建一个函数,该函数从创建数据库行/项目时起只需几秒钟。那么,如果数据库行是在 600 秒前创建的,这意味着该函数将输出“10 分钟前”?我希望它返回秒、分钟、小时、周、月或年。我知道它可能真的很简单,我只是似乎无法正确理解......

哦,这是一个 iPhone 应用程序,所以它使用 Objective-C。

任何帮助将不胜感激。

这就是我目前所拥有的:

-(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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太傻旳人生 2024-12-11 18:49:00

计算您的增量是多少秒。使用 NSDate dateWithIntervalSince1970 或 dateWithIntervalSinceReferenceDate 从秒值创建 NSDate 对象。您可以使用 NSDateFormatter 将该值格式化为小时/分钟/秒。月份和年份可能是错误的,但谁在乎呢?

要计算天和周,最简单的方法是将秒除以 24 * 60 * 60 得到天,然后除以 7 得到周。月份和年份是一个问题,因为它们是不规则的,因此如果您想要这种级别的分辨率,您可能最好使用日历计算。如果您知道某个内容是在 10000 秒前创建的,则可以使用 NSDate dateWithTimeIntervalSinceNow (具有负间隔)来生成该较早时间的日期对象。

[顺便说一句,你上面的代码似乎大致正确(我没有仔细检查)。我不明白为什么你不能通过一点努力让它发挥作用。]

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.]

羁客 2024-12-11 18:49:00

该文档有一整节关于 日历计算

如果您有创建日期 - 并且您有自创建日期以来经过的秒数 - 您可以使用该文档中描述的方法计算新日期。

The documentation has a whole section on Calendrical Calculations.

If you have the creation date - and you have the number of seconds elapsed since the creation date - you can calculate the new date by using the methods described in that document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文