Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string ToLongString(this TimeSpan time){ string output = String.Empty; if (time.Days > 0) output += time.Days + " days "; if ((time.Days == 0 || time.Days == 1) && time.Hours > 0) output += time.Hours + " hr "; if (time.Days == 0 && time.Minutes > 0) output += time.Minutes + " min "; if (output.Length == 0) output += time.Seconds + " sec"; return output.Trim();}
- public static class DateTimeHelper
- {
- private const int SECOND = 1;
- private const int MINUTE = 60 * SECOND;
- private const int HOUR = 60 * MINUTE;
- private const int DAY = 24 * HOUR;
- private const int MONTH = 30 * DAY;
- /// <summary>
- /// Returns a friendly version of the provided DateTime, relative to now. E.g.: "2 days ago", or "in 6 months".
- /// </summary>
- /// <param name="dateTime">The DateTime to compare to Now</param>
- /// <returns>A friendly string</returns>
- public static string GetFriendlyRelativeTime(DateTime dateTime)
- {
- if (DateTime.UtcNow.Ticks == dateTime.Ticks)
- {
- return "Right now!";
- }
- bool isFuture = (DateTime.UtcNow.Ticks < dateTime.Ticks);
- var ts = DateTime.UtcNow.Ticks < dateTime.Ticks ? new TimeSpan(dateTime.Ticks - DateTime.UtcNow.Ticks) : new TimeSpan(DateTime.UtcNow.Ticks - dateTime.Ticks);
- double delta = ts.TotalSeconds;
- if (delta < 1 * MINUTE)
- {
- return isFuture ? "in " + (ts.Seconds == 1 ? "one second" : ts.Seconds + " seconds") : ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
- }
- if (delta < 2 * MINUTE)
- {
- return isFuture ? "in a minute" : "a minute ago";
- }
- if (delta < 45 * MINUTE)
- {
- return isFuture ? "in " + ts.Minutes + " minutes" : ts.Minutes + " minutes ago";
- }
- if (delta < 90 * MINUTE)
- {
- return isFuture ? "in an hour" : "an hour ago";
- }
- if (delta < 24 * HOUR)
- {
- return isFuture ? "in " + ts.Hours + " hours" : ts.Hours + " hours ago";
- }
- if (delta < 48 * HOUR)
- {
- return isFuture ? "tomorrow" : "yesterday";
- }
- if (delta < 30 * DAY)
- {
- return isFuture ? "in " + ts.Days + " days" : ts.Days + " days ago";
- }
- if (delta < 12 * MONTH)
- {
- int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
- return isFuture ? "in " + (months <= 1 ? "one month" : months + " months") : months <= 1 ? "one month ago" : months + " months ago";
- }
- else
- {
- int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
- return isFuture ? "in " + (years <= 1 ? "one year" : years + " years") : years <= 1 ? "one year ago" : years + " years ago";
- }
- }
- }
- + (NSString *)timeAgoString:(NSDate *)date {
- int delta = -(int)[date timeIntervalSinceNow];
- if (delta < 60)
- {
- return delta == 1 ? @"one second ago" : [NSString stringWithFormat:@"%i seconds ago", delta];
- }
- if (delta < 120)
- {
- return @"a minute ago";
- }
- if (delta < 2700)
- {
- return [NSString stringWithFormat:@"%i minutes ago", delta/60];
- }
- if (delta < 5400)
- {
- return @"an hour ago";
- }
- if (delta < 24 * 3600)
- {
- return [NSString stringWithFormat:@"%i hours ago", delta/3600];
- }
- if (delta < 48 * 3600)
- {
- return @"yesterday";
- }
- if (delta < 30 * 24 * 3600)
- {
- return [NSString stringWithFormat:@"%i days ago", delta/(24*3600)];
- }
- if (delta < 12 * 30 * 24 * 3600)
- {
- int months = delta/(30*24*3600);
- return months <= 1 ? @"one month ago" : [NSString stringWithFormat:@"%i months ago", months];
- }
- else
- {
- int years = delta/(12*30*24*3600);
- return years <= 1 ? @"one year ago" : [NSString stringWithFormat:@"%i years ago", years];
- }
- 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;
- }
- var dateTime1 = 2.Hours().Ago();
- var dateTime2 = 3.Days().Ago();
- var dateTime3 = 1.Months().Ago();
- var dateTime4 = 5.Hours().FromNow();
- var dateTime5 = 2.Weeks().FromNow();
- var dateTime6 = 40.Seconds().FromNow();
- public class RelativeTimeRange : IComparable{ public TimeSpan UpperBound { get; set; } public delegate string RelativeTimeTextDelegate(TimeSpan timeDelta); public RelativeTimeTextDelegate MessageCreator { get; set; } public int CompareTo(object obj) { if (!(obj is RelativeTimeRange)) { return 1; } // note that this sorts in reverse order to the way you'd expect, // this saves having to reverse a list later return (obj as RelativeTimeRange).UpperBound.CompareTo(UpperBound); }}public class PrintRelativeTime{ private static List<RelativeTimeRange> timeRanges; static PrintRelativeTime() { timeRanges = new List<RelativeTimeRange>{ new RelativeTimeRange { UpperBound = TimeSpan.FromSeconds(1), MessageCreator = (delta) => { return "one second ago"; } }, new RelativeTimeRange { UpperBound = TimeSpan.FromSeconds(60), MessageCreator = (delta) => { return delta.Seconds + " seconds ago"; } }, new RelativeTimeRange { UpperBound = TimeSpan.FromMinutes(2), MessageCreator = (delta) => { return "one minute ago"; } }, new RelativeTimeRange { UpperBound = TimeSpan.FromMinutes(60), MessageCreator = (delta) => { return delta.Minutes + " minutes ago"; } }, new RelativeTimeRange { UpperBound = TimeSpan.FromHours(2), MessageCreator = (delta) => { return "one hour ago"; } }, new RelativeTimeRange { UpperBound = TimeSpan.FromHours(24), MessageCreator = (delta) => { return delta.Hours + " hours ago"; } }, new RelativeTimeRange { UpperBound = TimeSpan.FromDays(2), MessageCreator = (delta) => { return "yesterday"; } }, new RelativeTimeRange { UpperBound = DateTime.Now.Subtract(DateTime.Now.AddMonths(-1)), MessageCreator = (delta) => { return delta.Days + " days ago"; } }, new RelativeTimeRange { UpperBound = DateTime.Now.Subtract(DateTime.Now.AddMonths(-2)), MessageCreator = (delta) => { return "one month ago"; } }, new RelativeTimeRange { UpperBound = DateTime.Now.Subtract(DateTime.Now.AddYears(-1)), MessageCreator = (delta) => { return (int)Math.Floor(delta.TotalDays / 30) + " months ago"; } }, new RelativeTimeRange { UpperBound = DateTime.Now.Subtract(DateTime.Now.AddYears(-2)), MessageCreator = (delta) => { return "one year ago"; } }, new RelativeTimeRange { UpperBound = TimeSpan.MaxValue, MessageCreator = (delta) => { return (int)Math.Floor(delta.TotalDays / 365.24D) + " years ago"; } } }; timeRanges.Sort(); } public static string GetRelativeTimeMessage(TimeSpan ago) { RelativeTimeRange postRelativeDateRange = timeRanges[0]; foreach (var timeRange in timeRanges) { if (ago.CompareTo(timeRange.UpperBound) <= 0) { postRelativeDateRange = timeRange; } } return postRelativeDateRange.MessageCreator(ago); }}
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public static class RelativeDateHelper
- {
- private static Dictionary<double, Func<double, string>> sm_Dict = null;
- private static Dictionary<double, Func<double, string>> DictionarySetup()
- {
- var dict = new Dictionary<double, Func<double, string>>();
- dict.Add(0.75, (mins) => "less than a minute");
- dict.Add(1.5, (mins) => "about a minute");
- dict.Add(45, (mins) => string.Format("{0} minutes", Math.Round(mins)));
- dict.Add(90, (mins) => "about an hour");
- dict.Add(1440, (mins) => string.Format("about {0} hours", Math.Round(Math.Abs(mins / 60)))); // 60 * 24
- dict.Add(2880, (mins) => "a day"); // 60 * 48
- dict.Add(43200, (mins) => string.Format("{0} days", Math.Floor(Math.Abs(mins / 1440)))); // 60 * 24 * 30
- dict.Add(86400, (mins) => "about a month"); // 60 * 24 * 60
- dict.Add(525600, (mins) => string.Format("{0} months", Math.Floor(Math.Abs(mins / 43200)))); // 60 * 24 * 365
- dict.Add(1051200, (mins) => "about a year"); // 60 * 24 * 365 * 2
- dict.Add(double.MaxValue, (mins) => string.Format("{0} years", Math.Floor(Math.Abs(mins / 525600))));
- return dict;
- }
- public static string ToRelativeDate(this DateTime input)
- {
- TimeSpan oSpan = DateTime.Now.Subtract(input);
- double TotalMinutes = oSpan.TotalMinutes;
- string Suffix = " ago";
- if (TotalMinutes < 0.0)
- {
- TotalMinutes = Math.Abs(TotalMinutes);
- Suffix = " from now";
- }
- if (null == sm_Dict)
- sm_Dict = DictionarySetup();
- return sm_Dict.First(n => TotalMinutes < n.Key).Value.Invoke(TotalMinutes) + Suffix;
- }
- }
- import java.util.Date;
- public class RelativeDateFormat {
- private static final long ONE_MINUTE = 60000L;
- private static final long ONE_HOUR = 3600000L;
- private static final long ONE_DAY = 86400000L;
- private static final long ONE_WEEK = 604800000L;
- public static String format(Date date) {
- long delta = new Date().getTime() - date.getTime();
- if (delta < 1L * ONE_MINUTE) {
- return toSeconds(delta) == 1 ? "one second ago" : toSeconds(delta)
- + " seconds ago";
- }
- if (delta < 2L * ONE_MINUTE) {
- return "one minute ago";
- }
- if (delta < 45L * ONE_MINUTE) {
- return toMinutes(delta) + " minutes ago";
- }
- if (delta < 90L * ONE_MINUTE) {
- return "one hour ago";
- }
- if (delta < 24L * ONE_HOUR) {
- return toHours(delta) + " hours ago";
- }
- if (delta < 48L * ONE_HOUR) {
- return "yesterday";
- }
- if (delta < 30L * ONE_DAY) {
- return toDays(delta) + " days ago";
- }
- if (delta < 12L * 4L * ONE_WEEK) {
- 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 static long toSeconds(long date) {
- return date / 1000L;
- }
- private static long toMinutes(long date) {
- return toSeconds(date) / 60L;
- }
- private static long toHours(long date) {
- return toMinutes(date) / 60L;
- }
- private static long toDays(long date) {
- return toHours(date) / 24L;
- }
- private static long toMonths(long date) {
- return toDays(date) / 30L;
- }
- private static long toYears(long date) {
- return toMonths(date) / 365L;
- }
- }
- public string GetRelativeTime(DateTime timeStamp)
- {
- return string.Format("<script>printdate({0});</script>", timeStamp.ToFileTimeUtc());
- }
- long delta = new Date().getTime() - date.getTime();
- const int SECOND = 1;
- const int MINUTE = 60 * SECOND;
- const int HOUR = 60 * MINUTE;
- const int DAY = 24 * HOUR;
- const int MONTH = 30 * DAY;
- if (delta < 0L)
- {
- return "not yet";
- }
- if (delta < 1L * MINUTE)
- {
- return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
- }
- if (delta < 2L * MINUTE)
- {
- return "a minute ago";
- }
- if (delta < 45L * MINUTE)
- {
- return ts.Minutes + " minutes ago";
- }
- if (delta < 90L * MINUTE)
- {
- return "an hour ago";
- }
- if (delta < 24L * HOUR)
- {
- return ts.Hours + " hours ago";
- }
- if (delta < 48L * HOUR)
- {
- return "yesterday";
- }
- if (delta < 30L * DAY)
- {
- return ts.Days + " days ago";
- }
- if (delta < 12L * MONTH)
- {
- int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
- return months <= 1 ? "one month ago" : months + " months ago";
- }
- else
- {
- int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
- return years <= 1 ? "one year ago" : years + " years ago";
- }
- agoify($delta)
- local($y, $mo, $d, $h, $m, $s);
- $s = floor($delta);
- if($s<=1) return "a second ago";
- if($s<60) return "$s seconds ago";
- $m = floor($s/60);
- if($m==1) return "a minute ago";
- if($m<45) return "$m minutes ago";
- $h = floor($m/60);
- if($h==1) return "an hour ago";
- if($h<24) return "$h hours ago";
- $d = floor($h/24);
- if($d<2) return "yesterday";
- if($d<30) return "$d days ago";
- $mo = floor($d/30);
- if($mo<=1) return "a month ago";
- $y = floor($mo/12);
- if($y<1) return "$mo months ago";
- if($y==1) return "a year ago";
- return "$y years ago";
- var ts = new TimeSpan(DateTime.Now.Ticks - dt.Ticks);
- (DateTime.UtcNow - dt).TotalSeconds
- DateTime.UtcNow.AddHours(-30).Humanize() => "yesterday"
- DateTime.UtcNow.AddHours(-2).Humanize() => "2 hours ago"
- DateTime.UtcNow.AddHours(30).Humanize() => "tomorrow"
- DateTime.UtcNow.AddHours(2).Humanize() => "2 hours from now"
- TimeSpan.FromMilliseconds(1299630020).Humanize() => "2 weeks"
- TimeSpan.FromMilliseconds(1299630020).Humanize(3) => "2 weeks, 1 day, 1 hour"
- if (delta < 5400) // 90 * 60
- {
- return "an hour ago";
- }
- if (delta < 7200) // 120 * 60
- {
- return "an hour ago";
- }
- function posted(t) {
- var now = new Date();
- var diff = parseInt((now.getTime() - Date.parse(t)) / 1000);
- if (diff < 60) { return 'less than a minute ago'; }
- else if (diff < 120) { return 'about a minute ago'; }
- else if (diff < (2700)) { return (parseInt(diff / 60)).toString() + ' minutes ago'; }
- else if (diff < (5400)) { return 'about an hour ago'; }
- else if (diff < (86400)) { return 'about ' + (parseInt(diff / 3600)).toString() + ' hours ago'; }
- else if (diff < (172800)) { return '1 day ago'; }
- else {return (parseInt(diff / 86400)).toString() + ' days ago'; }
- }
- public static class TimeSpanExtensions {
- public static TimeSpan Days(this int value) {
- return new TimeSpan(value, 0, 0, 0);
- }
- public static TimeSpan Hours(this int value) {
- return new TimeSpan(0, value, 0, 0);
- }
- public static TimeSpan Minutes(this int value) {
- return new TimeSpan(0, 0, value, 0);
- }
- public static TimeSpan Seconds(this int value) {
- return new TimeSpan(0, 0, 0, value);
- }
- public static TimeSpan Milliseconds(this int value) {
- return new TimeSpan(0, 0, 0, 0, value);
- }
- public static DateTime Ago(this TimeSpan value) {
- return DateTime.Now - value;
- }
- }
- public static class DateTimeExtensions {
- public static DateTime Ago(this DateTime dateTime, TimeSpan delta) {
- return dateTime - delta;
- }
- }
- var date = DateTime.Now;
- date.Ago(2.Days()); // 2 days ago
- date.Ago(7.Hours()); // 7 hours ago
- date.Ago(567.Milliseconds()); // 567 milliseconds ago
- public string getRelativeDateTime(DateTime date)
- {
- TimeSpan ts = DateTime.Now - date;
- if (ts.TotalMinutes < 1)//seconds ago
- return "just now";
- if (ts.TotalHours < 1)//min ago
- return (int)ts.TotalMinutes == 1 ? "1 Minute ago" : (int)ts.TotalMinutes + " Minutes ago";
- if (ts.TotalDays < 1)//hours ago
- return (int)ts.TotalHours == 1 ? "1 Hour ago" : (int)ts.TotalHours + " Hours ago";
- if (ts.TotalDays < 7)//days ago
- return (int)ts.TotalDays == 1 ? "1 Day ago" : (int)ts.TotalDays + " Days ago";
- if (ts.TotalDays < 30.4368)//weeks ago
- return (int)(ts.TotalDays / 7) == 1 ? "1 Week ago" : (int)(ts.TotalDays / 7) + " Weeks ago";
- if (ts.TotalDays < 365.242)//months ago
- return (int)(ts.TotalDays / 30.4368) == 1 ? "1 Month ago" : (int)(ts.TotalDays / 30.4368) + " Months ago";
- //years ago
- return (int)(ts.TotalDays / 365.242) == 1 ? "1 Year ago" : (int)(ts.TotalDays / 365.242) + " Years ago";
- }
- public string RelativeDateTimeCount(DateTime inputDateTime)
- {
- string outputDateTime = string.Empty;
- TimeSpan ts = DateTime.Now - inputDateTime;
- if (ts.Days > 7)
- { outputDateTime = inputDateTime.ToString("MMMM d, yyyy"); }
- else if (ts.Days > 0)
- {
- outputDateTime = ts.Days == 1 ? ("about 1 Day ago") : ("about " + ts.Days.ToString() + " Days ago");
- }
- else if (ts.Hours > 0)
- {
- outputDateTime = ts.Hours == 1 ? ("an hour ago") : (ts.Hours.ToString() + " hours ago");
- }
- else if (ts.Minutes > 0)
- {
- outputDateTime = ts.Minutes == 1 ? ("1 minute ago") : (ts.Minutes.ToString() + " minutes ago");
- }
- else outputDateTime = "few seconds ago";
- return outputDateTime;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement