relative-time-span

How to subtract dates from each other

不打扰是莪最后的温柔 提交于 2019-12-24 13:15:11
问题 I am using Groovy. I have parsed a textfile whose lines contain information, including dates. I now have just the dates, for example: 08:13:16,121 09:32:42,102 10:43:47,153 I want to compare the deltas between these values; how can I do this? i.e, I want to subtract the first from the second, and compare that value to the difference between the third and the second. I will save the largest value. 回答1: Assuming your times are in a file times.txt , you can do this: def parseDate = { str -> new

How to calculate “time ago” in Java?

懵懂的女人 提交于 2019-11-26 03:16:40
问题 In Ruby on Rails, there is a feature that allows you to take any Date and print out how \"long ago\" it was. For example: 8 minutes ago 8 hours ago 8 days ago 8 months ago 8 years ago Is there an easy way to do this in Java? 回答1: Take a look at the PrettyTime library. It's quite simple to use: import org.ocpsoft.prettytime.PrettyTime; PrettyTime p = new PrettyTime(); System.out.println(p.format(new Date())); // prints "moments ago" You can also pass in a locale for internationalized messages:

PHP How to find the time elapsed since a date time? [duplicate]

我的未来我决定 提交于 2019-11-26 00:14:13
问题 This question already has answers here : Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago… (28 answers) Closed 4 years ago . How to find the time elapsed since a date time stamp like 2010-04-28 17:25:43 , final out put text should be like xx Minutes Ago / xx Days Ago 回答1: Most of the answers seem focused around converting the date from a string to time. It seems you're mostly thinking about getting the date into the '5 days ago' format, etc.. right? This is how I'd go about

Calculate relative time in C#

不问归期 提交于 2019-11-25 21:41:46
问题 Given a specific DateTime value, how do I display relative time, like: 2 hours ago 3 days ago a month ago 回答1: Jeff, your code is nice but could be clearer with constants (as suggested in Code Complete). const int SECOND = 1; const int MINUTE = 60 * SECOND; const int HOUR = 60 * MINUTE; const int DAY = 24 * HOUR; const int MONTH = 30 * DAY; var ts = new TimeSpan(DateTime.UtcNow.Ticks - yourDate.Ticks); double delta = Math.Abs(ts.TotalSeconds); if (delta < 1 * MINUTE) return ts.Seconds == 1 ?