ant-design-blazor/components/core/Helpers/MomentHelper.cs
Zhuangkh 225358982e feat: add comment component (#80)
* feat:add AntComment

* refactor(comment): change namespcae to AntDesign

* docs: fix comment de
2020-06-30 15:25:21 +08:00

47 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace AntDesign
{
public static class MomentHelper
{
private const int DaysPerYear = 365;
private const int DaysPerMonth = 31;
private const int DaysPerWeek = 7;
private const int HoursPerDay = 24;
public static string FromNow(DateTime time )
{
var timespan = DateTime.Now - time;
if (timespan.TotalDays >= DaysPerYear)
{
return $"{Math.Floor(timespan.TotalDays / DaysPerYear)} years ago";
}
if (timespan.TotalDays >= DaysPerMonth)
{
return $"{Math.Floor(timespan.TotalDays / DaysPerMonth)} months ago";
}
if (timespan.TotalDays >= DaysPerWeek)
{
return $"{Math.Floor(timespan.TotalDays / DaysPerWeek)} weeks ago";
}
if (timespan.TotalHours >= HoursPerDay)
{
return $"{Math.Floor(timespan.TotalDays)} days ago";
}
if (timespan.TotalHours >= 1)
{
return $"{Math.Floor(timespan.TotalHours)} hours ago";
}
if (timespan.TotalMinutes >= 1)
{
return $"{Math.Floor(timespan.TotalMinutes)} minutes ago";
}
return "a few seconds ago";
}
}
}