给定特定的DateTime值,如何显示相对时间,例如:
2小时前3天前一个月前
给定特定的DateTime值,如何显示相对时间,例如:
2小时前3天前一个月前
当前回答
通过在客户端执行此逻辑,可以减少服务器端负载。在一些Digg页面上查看源代码以供参考。它们让服务器发出一个由Javascript处理的历元时间值。这样,您就不需要管理最终用户的时区。新的服务器端代码类似于:
public string GetRelativeTime(DateTime timeStamp)
{
return string.Format("<script>printdate({0});</script>", timeStamp.ToFileTimeUtc());
}
您甚至可以在那里添加一个NOSCRIPT块,然后执行ToString()。
其他回答
我是这样做的
var ts = new TimeSpan(DateTime.UtcNow.Ticks - dt.Ticks);
double delta = Math.Abs(ts.TotalSeconds);
if (delta < 60)
{
return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
}
if (delta < 60 * 2)
{
return "a minute ago";
}
if (delta < 45 * 60)
{
return ts.Minutes + " minutes ago";
}
if (delta < 90 * 60)
{
return "an hour ago";
}
if (delta < 24 * 60 * 60)
{
return ts.Hours + " hours ago";
}
if (delta < 48 * 60 * 60)
{
return "yesterday";
}
if (delta < 30 * 24 * 60 * 60)
{
return ts.Days + " days ago";
}
if (delta < 12 * 30 * 24 * 60 * 60)
{
int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
return months <= 1 ? "one month ago" : months + " months ago";
}
int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
return years <= 1 ? "one year ago" : years + " years ago";
建议?评论?如何改进此算法?
var ts = new TimeSpan(DateTime.Now.Ticks - dt.Ticks);
文森特接受的答案做出了许多武断的决定。为什么45分钟舍入为一小时,而45秒不舍入为一分钟?在年和月的计算中,它的圈复杂度增加了,这使得遵循逻辑变得更加复杂。它假设TimeSpan是相对于过去(2天前)的,而它很可能是在未来(2天后)。它定义了不必要的常量,而不是使用TimeSpan.TicksPerSecond等。
此实现解决了上述问题,并更新了语法以使用开关表达式和关系模式
/// <summary>
/// Convert a <see cref="TimeSpan"/> to a natural language representation.
/// </summary>
/// <example>
/// <code>
/// TimeSpan.FromSeconds(10).ToNaturalLanguage();
/// // 10 seconds
/// </code>
/// </example>
public static string ToNaturalLanguage(this TimeSpan @this)
{
const int daysInWeek = 7;
const int daysInMonth = 30;
const int daysInYear = 365;
const long threshold = 100 * TimeSpan.TicksPerMillisecond;
@this = @this.TotalSeconds < 0
? TimeSpan.FromSeconds(@this.TotalSeconds * -1)
: @this;
return (@this.Ticks + threshold) switch
{
< 2 * TimeSpan.TicksPerSecond => "a second",
< 1 * TimeSpan.TicksPerMinute => @this.Seconds + " seconds",
< 2 * TimeSpan.TicksPerMinute => "a minute",
< 1 * TimeSpan.TicksPerHour => @this.Minutes + " minutes",
< 2 * TimeSpan.TicksPerHour => "an hour",
< 1 * TimeSpan.TicksPerDay => @this.Hours + " hours",
< 2 * TimeSpan.TicksPerDay => "a day",
< 1 * daysInWeek * TimeSpan.TicksPerDay => @this.Days + " days",
< 2 * daysInWeek * TimeSpan.TicksPerDay => "a week",
< 1 * daysInMonth * TimeSpan.TicksPerDay => (@this.Days / daysInWeek).ToString("F0") + " weeks",
< 2 * daysInMonth * TimeSpan.TicksPerDay => "a month",
< 1 * daysInYear * TimeSpan.TicksPerDay => (@this.Days / daysInMonth).ToString("F0") + " months",
< 2 * daysInYear * TimeSpan.TicksPerDay => "a year",
_ => (@this.Days / daysInYear).ToString("F0") + " years"
};
}
/// <summary>
/// Convert a <see cref="DateTime"/> to a natural language representation.
/// </summary>
/// <example>
/// <code>
/// (DateTime.Now - TimeSpan.FromSeconds(10)).ToNaturalLanguage()
/// // 10 seconds ago
/// </code>
/// </example>
public static string ToNaturalLanguage(this DateTime @this)
{
TimeSpan timeSpan = @this - DateTime.Now;
return timeSpan.TotalSeconds switch
{
>= 1 => timeSpan.ToNaturalLanguage() + " until",
<= -1 => timeSpan.ToNaturalLanguage() + " ago",
_ => "now",
};
}
可以使用NUnit对其进行如下测试:
[TestCase("a second", 0)]
[TestCase("a second", 1)]
[TestCase("2 seconds", 2)]
[TestCase("a minute", 0, 1)]
[TestCase("5 minutes", 0, 5)]
[TestCase("an hour", 0, 0, 1)]
[TestCase("2 hours", 0, 0, 2)]
[TestCase("a day", 0, 0, 24)]
[TestCase("a day", 0, 0, 0, 1)]
[TestCase("6 days", 0, 0, 0, 6)]
[TestCase("a week", 0, 0, 0, 7)]
[TestCase("4 weeks", 0, 0, 0, 29)]
[TestCase("a month", 0, 0, 0, 30)]
[TestCase("6 months", 0, 0, 0, 6 * 30)]
[TestCase("a year", 0, 0, 0, 365)]
[TestCase("68 years", int.MaxValue)]
public void NaturalLanguageHelpers_TimeSpan(
string expected,
int seconds,
int minutes = 0,
int hours = 0,
int days = 0
)
{
// Arrange
TimeSpan timeSpan = new(days, hours, minutes, seconds);
// Act
string result = timeSpan.ToNaturalLanguage();
// Assert
Assert.That(result, Is.EqualTo(expected));
}
[TestCase("now", 0)]
[TestCase("10 minutes ago", 0, -10)]
[TestCase("10 minutes until", 10, 10)]
[TestCase("68 years until", int.MaxValue)]
[TestCase("68 years ago", int.MinValue)]
public void NaturalLanguageHelpers_DateTime(
string expected,
int seconds,
int minutes = 0,
int hours = 0,
int days = 0
)
{
// Arrange
TimeSpan timeSpan = new(days, hours, minutes, seconds);
DateTime now = DateTime.Now;
DateTime dateTime = now + timeSpan;
// Act
string result = dateTime.ToNaturalLanguage();
// Assert
Assert.That(result, Is.EqualTo(expected));
}
或者作为要点:https://gist.github.com/StudioLE/2dd394e3f792e79adc927ede274df56e
在Java中有没有一种简单的方法可以做到这一点?java.util.Date类似乎相当有限。
下面是我的快速而肮脏的Java解决方案:
import java.util.Date;
import javax.management.timer.Timer;
String getRelativeDate(Date date) {
long delta = new Date().getTime() - date.getTime();
if (delta < 1L * Timer.ONE_MINUTE) {
return toSeconds(delta) == 1 ? "one second ago" : toSeconds(delta) + " seconds ago";
}
if (delta < 2L * Timer.ONE_MINUTE) {
return "a minute ago";
}
if (delta < 45L * Timer.ONE_MINUTE) {
return toMinutes(delta) + " minutes ago";
}
if (delta < 90L * Timer.ONE_MINUTE) {
return "an hour ago";
}
if (delta < 24L * Timer.ONE_HOUR) {
return toHours(delta) + " hours ago";
}
if (delta < 48L * Timer.ONE_HOUR) {
return "yesterday";
}
if (delta < 30L * Timer.ONE_DAY) {
return toDays(delta) + " days ago";
}
if (delta < 12L * 4L * Timer.ONE_WEEK) { // a month
long months = toMonths(delta);
return months <= 1 ? "one month ago" : months + " months ago";
}
else {
long years = toYears(delta);
return years <= 1 ? "one year ago" : years + " years ago";
}
}
private long toSeconds(long date) {
return date / 1000L;
}
private long toMinutes(long date) {
return toSeconds(date) / 60L;
}
private long toHours(long date) {
return toMinutes(date) / 60L;
}
private long toDays(long date) {
return toHours(date) / 24L;
}
private long toMonths(long date) {
return toDays(date) / 30L;
}
private long toYears(long date) {
return toMonths(date) / 365L;
}
以某种方式,您可以使用DateTime函数以秒到年计算相对时间,请尝试以下操作:
using System;
public class Program {
public static string getRelativeTime(DateTime past) {
DateTime now = DateTime.Today;
string rt = "";
int time;
string statement = "";
if (past.Second >= now.Second) {
if (past.Second - now.Second == 1) {
rt = "second ago";
}
rt = "seconds ago";
time = past.Second - now.Second;
statement = "" + time;
return (statement + rt);
}
if (past.Minute >= now.Minute) {
if (past.Second - now.Second == 1) {
rt = "second ago";
} else {
rt = "minutes ago";
}
time = past.Minute - now.Minute;
statement = "" + time;
return (statement + rt);
}
// This process will go on until years
}
public static void Main() {
DateTime before = new DateTime(1995, 8, 24);
string date = getRelativeTime(before);
Console.WriteLine("Windows 95 was {0}.", date);
}
}
不完全有效,但如果您对其进行一点修改和调试,它很可能会完成任务。