Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.28 KB | None | 0 0
  1. 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();}
  2.  
  3. public static class DateTimeHelper
  4. {
  5. private const int SECOND = 1;
  6. private const int MINUTE = 60 * SECOND;
  7. private const int HOUR = 60 * MINUTE;
  8. private const int DAY = 24 * HOUR;
  9. private const int MONTH = 30 * DAY;
  10.  
  11. /// <summary>
  12. /// Returns a friendly version of the provided DateTime, relative to now. E.g.: "2 days ago", or "in 6 months".
  13. /// </summary>
  14. /// <param name="dateTime">The DateTime to compare to Now</param>
  15. /// <returns>A friendly string</returns>
  16. public static string GetFriendlyRelativeTime(DateTime dateTime)
  17. {
  18. if (DateTime.UtcNow.Ticks == dateTime.Ticks)
  19. {
  20. return "Right now!";
  21. }
  22.  
  23. bool isFuture = (DateTime.UtcNow.Ticks < dateTime.Ticks);
  24. var ts = DateTime.UtcNow.Ticks < dateTime.Ticks ? new TimeSpan(dateTime.Ticks - DateTime.UtcNow.Ticks) : new TimeSpan(DateTime.UtcNow.Ticks - dateTime.Ticks);
  25.  
  26. double delta = ts.TotalSeconds;
  27.  
  28. if (delta < 1 * MINUTE)
  29. {
  30. return isFuture ? "in " + (ts.Seconds == 1 ? "one second" : ts.Seconds + " seconds") : ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
  31. }
  32. if (delta < 2 * MINUTE)
  33. {
  34. return isFuture ? "in a minute" : "a minute ago";
  35. }
  36. if (delta < 45 * MINUTE)
  37. {
  38. return isFuture ? "in " + ts.Minutes + " minutes" : ts.Minutes + " minutes ago";
  39. }
  40. if (delta < 90 * MINUTE)
  41. {
  42. return isFuture ? "in an hour" : "an hour ago";
  43. }
  44. if (delta < 24 * HOUR)
  45. {
  46. return isFuture ? "in " + ts.Hours + " hours" : ts.Hours + " hours ago";
  47. }
  48. if (delta < 48 * HOUR)
  49. {
  50. return isFuture ? "tomorrow" : "yesterday";
  51. }
  52. if (delta < 30 * DAY)
  53. {
  54. return isFuture ? "in " + ts.Days + " days" : ts.Days + " days ago";
  55. }
  56. if (delta < 12 * MONTH)
  57. {
  58. int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
  59. return isFuture ? "in " + (months <= 1 ? "one month" : months + " months") : months <= 1 ? "one month ago" : months + " months ago";
  60. }
  61. else
  62. {
  63. int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
  64. return isFuture ? "in " + (years <= 1 ? "one year" : years + " years") : years <= 1 ? "one year ago" : years + " years ago";
  65. }
  66. }
  67. }
  68.  
  69. + (NSString *)timeAgoString:(NSDate *)date {
  70. int delta = -(int)[date timeIntervalSinceNow];
  71.  
  72. if (delta < 60)
  73. {
  74. return delta == 1 ? @"one second ago" : [NSString stringWithFormat:@"%i seconds ago", delta];
  75. }
  76. if (delta < 120)
  77. {
  78. return @"a minute ago";
  79. }
  80. if (delta < 2700)
  81. {
  82. return [NSString stringWithFormat:@"%i minutes ago", delta/60];
  83. }
  84. if (delta < 5400)
  85. {
  86. return @"an hour ago";
  87. }
  88. if (delta < 24 * 3600)
  89. {
  90. return [NSString stringWithFormat:@"%i hours ago", delta/3600];
  91. }
  92. if (delta < 48 * 3600)
  93. {
  94. return @"yesterday";
  95. }
  96. if (delta < 30 * 24 * 3600)
  97. {
  98. return [NSString stringWithFormat:@"%i days ago", delta/(24*3600)];
  99. }
  100. if (delta < 12 * 30 * 24 * 3600)
  101. {
  102. int months = delta/(30*24*3600);
  103. return months <= 1 ? @"one month ago" : [NSString stringWithFormat:@"%i months ago", months];
  104. }
  105. else
  106. {
  107. int years = delta/(12*30*24*3600);
  108. return years <= 1 ? @"one year ago" : [NSString stringWithFormat:@"%i years ago", years];
  109. }
  110.  
  111. import java.util.Date;
  112. import javax.management.timer.Timer;
  113.  
  114. String getRelativeDate(Date date) {
  115. long delta = new Date().getTime() - date.getTime();
  116. if (delta < 1L * Timer.ONE_MINUTE) {
  117. return toSeconds(delta) == 1 ? "one second ago" : toSeconds(delta) + " seconds ago";
  118. }
  119. if (delta < 2L * Timer.ONE_MINUTE) {
  120. return "a minute ago";
  121. }
  122. if (delta < 45L * Timer.ONE_MINUTE) {
  123. return toMinutes(delta) + " minutes ago";
  124. }
  125. if (delta < 90L * Timer.ONE_MINUTE) {
  126. return "an hour ago";
  127. }
  128. if (delta < 24L * Timer.ONE_HOUR) {
  129. return toHours(delta) + " hours ago";
  130. }
  131. if (delta < 48L * Timer.ONE_HOUR) {
  132. return "yesterday";
  133. }
  134. if (delta < 30L * Timer.ONE_DAY) {
  135. return toDays(delta) + " days ago";
  136. }
  137. if (delta < 12L * 4L * Timer.ONE_WEEK) { // a month
  138. long months = toMonths(delta);
  139. return months <= 1 ? "one month ago" : months + " months ago";
  140. }
  141. else {
  142. long years = toYears(delta);
  143. return years <= 1 ? "one year ago" : years + " years ago";
  144. }
  145. }
  146.  
  147. private long toSeconds(long date) {
  148. return date / 1000L;
  149. }
  150.  
  151. private long toMinutes(long date) {
  152. return toSeconds(date) / 60L;
  153. }
  154.  
  155. private long toHours(long date) {
  156. return toMinutes(date) / 60L;
  157. }
  158.  
  159. private long toDays(long date) {
  160. return toHours(date) / 24L;
  161. }
  162.  
  163. private long toMonths(long date) {
  164. return toDays(date) / 30L;
  165. }
  166.  
  167. private long toYears(long date) {
  168. return toMonths(date) / 365L;
  169. }
  170.  
  171. var dateTime1 = 2.Hours().Ago();
  172. var dateTime2 = 3.Days().Ago();
  173. var dateTime3 = 1.Months().Ago();
  174. var dateTime4 = 5.Hours().FromNow();
  175. var dateTime5 = 2.Weeks().FromNow();
  176. var dateTime6 = 40.Seconds().FromNow();
  177.  
  178. 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); }}
  179.  
  180. using System;
  181. using System.Collections.Generic;
  182. using System.Linq;
  183.  
  184. public static class RelativeDateHelper
  185. {
  186. private static Dictionary<double, Func<double, string>> sm_Dict = null;
  187.  
  188. private static Dictionary<double, Func<double, string>> DictionarySetup()
  189. {
  190. var dict = new Dictionary<double, Func<double, string>>();
  191. dict.Add(0.75, (mins) => "less than a minute");
  192. dict.Add(1.5, (mins) => "about a minute");
  193. dict.Add(45, (mins) => string.Format("{0} minutes", Math.Round(mins)));
  194. dict.Add(90, (mins) => "about an hour");
  195. dict.Add(1440, (mins) => string.Format("about {0} hours", Math.Round(Math.Abs(mins / 60)))); // 60 * 24
  196. dict.Add(2880, (mins) => "a day"); // 60 * 48
  197. dict.Add(43200, (mins) => string.Format("{0} days", Math.Floor(Math.Abs(mins / 1440)))); // 60 * 24 * 30
  198. dict.Add(86400, (mins) => "about a month"); // 60 * 24 * 60
  199. dict.Add(525600, (mins) => string.Format("{0} months", Math.Floor(Math.Abs(mins / 43200)))); // 60 * 24 * 365
  200. dict.Add(1051200, (mins) => "about a year"); // 60 * 24 * 365 * 2
  201. dict.Add(double.MaxValue, (mins) => string.Format("{0} years", Math.Floor(Math.Abs(mins / 525600))));
  202.  
  203. return dict;
  204. }
  205.  
  206. public static string ToRelativeDate(this DateTime input)
  207. {
  208. TimeSpan oSpan = DateTime.Now.Subtract(input);
  209. double TotalMinutes = oSpan.TotalMinutes;
  210. string Suffix = " ago";
  211.  
  212. if (TotalMinutes < 0.0)
  213. {
  214. TotalMinutes = Math.Abs(TotalMinutes);
  215. Suffix = " from now";
  216. }
  217.  
  218. if (null == sm_Dict)
  219. sm_Dict = DictionarySetup();
  220.  
  221. return sm_Dict.First(n => TotalMinutes < n.Key).Value.Invoke(TotalMinutes) + Suffix;
  222. }
  223. }
  224.  
  225. import java.util.Date;
  226.  
  227. public class RelativeDateFormat {
  228.  
  229. private static final long ONE_MINUTE = 60000L;
  230. private static final long ONE_HOUR = 3600000L;
  231. private static final long ONE_DAY = 86400000L;
  232. private static final long ONE_WEEK = 604800000L;
  233.  
  234. public static String format(Date date) {
  235.  
  236. long delta = new Date().getTime() - date.getTime();
  237. if (delta < 1L * ONE_MINUTE) {
  238. return toSeconds(delta) == 1 ? "one second ago" : toSeconds(delta)
  239. + " seconds ago";
  240. }
  241. if (delta < 2L * ONE_MINUTE) {
  242. return "one minute ago";
  243. }
  244. if (delta < 45L * ONE_MINUTE) {
  245. return toMinutes(delta) + " minutes ago";
  246. }
  247. if (delta < 90L * ONE_MINUTE) {
  248. return "one hour ago";
  249. }
  250. if (delta < 24L * ONE_HOUR) {
  251. return toHours(delta) + " hours ago";
  252. }
  253. if (delta < 48L * ONE_HOUR) {
  254. return "yesterday";
  255. }
  256. if (delta < 30L * ONE_DAY) {
  257. return toDays(delta) + " days ago";
  258. }
  259. if (delta < 12L * 4L * ONE_WEEK) {
  260. long months = toMonths(delta);
  261. return months <= 1 ? "one month ago" : months + " months ago";
  262. } else {
  263. long years = toYears(delta);
  264. return years <= 1 ? "one year ago" : years + " years ago";
  265. }
  266. }
  267.  
  268. private static long toSeconds(long date) {
  269. return date / 1000L;
  270. }
  271.  
  272. private static long toMinutes(long date) {
  273. return toSeconds(date) / 60L;
  274. }
  275.  
  276. private static long toHours(long date) {
  277. return toMinutes(date) / 60L;
  278. }
  279.  
  280. private static long toDays(long date) {
  281. return toHours(date) / 24L;
  282. }
  283.  
  284. private static long toMonths(long date) {
  285. return toDays(date) / 30L;
  286. }
  287.  
  288. private static long toYears(long date) {
  289. return toMonths(date) / 365L;
  290. }
  291.  
  292. }
  293.  
  294. public string GetRelativeTime(DateTime timeStamp)
  295. {
  296. return string.Format("<script>printdate({0});</script>", timeStamp.ToFileTimeUtc());
  297. }
  298.  
  299. long delta = new Date().getTime() - date.getTime();
  300. const int SECOND = 1;
  301. const int MINUTE = 60 * SECOND;
  302. const int HOUR = 60 * MINUTE;
  303. const int DAY = 24 * HOUR;
  304. const int MONTH = 30 * DAY;
  305.  
  306. if (delta < 0L)
  307. {
  308. return "not yet";
  309. }
  310. if (delta < 1L * MINUTE)
  311. {
  312. return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
  313. }
  314. if (delta < 2L * MINUTE)
  315. {
  316. return "a minute ago";
  317. }
  318. if (delta < 45L * MINUTE)
  319. {
  320. return ts.Minutes + " minutes ago";
  321. }
  322. if (delta < 90L * MINUTE)
  323. {
  324. return "an hour ago";
  325. }
  326. if (delta < 24L * HOUR)
  327. {
  328. return ts.Hours + " hours ago";
  329. }
  330. if (delta < 48L * HOUR)
  331. {
  332. return "yesterday";
  333. }
  334. if (delta < 30L * DAY)
  335. {
  336. return ts.Days + " days ago";
  337. }
  338. if (delta < 12L * MONTH)
  339. {
  340. int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
  341. return months <= 1 ? "one month ago" : months + " months ago";
  342. }
  343. else
  344. {
  345. int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
  346. return years <= 1 ? "one year ago" : years + " years ago";
  347. }
  348.  
  349. agoify($delta)
  350. local($y, $mo, $d, $h, $m, $s);
  351. $s = floor($delta);
  352. if($s<=1) return "a second ago";
  353. if($s<60) return "$s seconds ago";
  354. $m = floor($s/60);
  355. if($m==1) return "a minute ago";
  356. if($m<45) return "$m minutes ago";
  357. $h = floor($m/60);
  358. if($h==1) return "an hour ago";
  359. if($h<24) return "$h hours ago";
  360. $d = floor($h/24);
  361. if($d<2) return "yesterday";
  362. if($d<30) return "$d days ago";
  363. $mo = floor($d/30);
  364. if($mo<=1) return "a month ago";
  365. $y = floor($mo/12);
  366. if($y<1) return "$mo months ago";
  367. if($y==1) return "a year ago";
  368. return "$y years ago";
  369.  
  370. var ts = new TimeSpan(DateTime.Now.Ticks - dt.Ticks);
  371.  
  372. (DateTime.UtcNow - dt).TotalSeconds
  373.  
  374. DateTime.UtcNow.AddHours(-30).Humanize() => "yesterday"
  375. DateTime.UtcNow.AddHours(-2).Humanize() => "2 hours ago"
  376.  
  377. DateTime.UtcNow.AddHours(30).Humanize() => "tomorrow"
  378. DateTime.UtcNow.AddHours(2).Humanize() => "2 hours from now"
  379.  
  380. TimeSpan.FromMilliseconds(1299630020).Humanize() => "2 weeks"
  381. TimeSpan.FromMilliseconds(1299630020).Humanize(3) => "2 weeks, 1 day, 1 hour"
  382.  
  383. if (delta < 5400) // 90 * 60
  384. {
  385. return "an hour ago";
  386. }
  387.  
  388. if (delta < 7200) // 120 * 60
  389. {
  390. return "an hour ago";
  391. }
  392.  
  393. function posted(t) {
  394. var now = new Date();
  395. var diff = parseInt((now.getTime() - Date.parse(t)) / 1000);
  396. if (diff < 60) { return 'less than a minute ago'; }
  397. else if (diff < 120) { return 'about a minute ago'; }
  398. else if (diff < (2700)) { return (parseInt(diff / 60)).toString() + ' minutes ago'; }
  399. else if (diff < (5400)) { return 'about an hour ago'; }
  400. else if (diff < (86400)) { return 'about ' + (parseInt(diff / 3600)).toString() + ' hours ago'; }
  401. else if (diff < (172800)) { return '1 day ago'; }
  402. else {return (parseInt(diff / 86400)).toString() + ' days ago'; }
  403. }
  404.  
  405. public static class TimeSpanExtensions {
  406.  
  407. public static TimeSpan Days(this int value) {
  408.  
  409. return new TimeSpan(value, 0, 0, 0);
  410. }
  411.  
  412. public static TimeSpan Hours(this int value) {
  413.  
  414. return new TimeSpan(0, value, 0, 0);
  415. }
  416.  
  417. public static TimeSpan Minutes(this int value) {
  418.  
  419. return new TimeSpan(0, 0, value, 0);
  420. }
  421.  
  422. public static TimeSpan Seconds(this int value) {
  423.  
  424. return new TimeSpan(0, 0, 0, value);
  425. }
  426.  
  427. public static TimeSpan Milliseconds(this int value) {
  428.  
  429. return new TimeSpan(0, 0, 0, 0, value);
  430. }
  431.  
  432. public static DateTime Ago(this TimeSpan value) {
  433.  
  434. return DateTime.Now - value;
  435. }
  436. }
  437.  
  438. public static class DateTimeExtensions {
  439.  
  440. public static DateTime Ago(this DateTime dateTime, TimeSpan delta) {
  441.  
  442. return dateTime - delta;
  443. }
  444. }
  445.  
  446. var date = DateTime.Now;
  447. date.Ago(2.Days()); // 2 days ago
  448. date.Ago(7.Hours()); // 7 hours ago
  449. date.Ago(567.Milliseconds()); // 567 milliseconds ago
  450.  
  451. public string getRelativeDateTime(DateTime date)
  452. {
  453. TimeSpan ts = DateTime.Now - date;
  454. if (ts.TotalMinutes < 1)//seconds ago
  455. return "just now";
  456. if (ts.TotalHours < 1)//min ago
  457. return (int)ts.TotalMinutes == 1 ? "1 Minute ago" : (int)ts.TotalMinutes + " Minutes ago";
  458. if (ts.TotalDays < 1)//hours ago
  459. return (int)ts.TotalHours == 1 ? "1 Hour ago" : (int)ts.TotalHours + " Hours ago";
  460. if (ts.TotalDays < 7)//days ago
  461. return (int)ts.TotalDays == 1 ? "1 Day ago" : (int)ts.TotalDays + " Days ago";
  462. if (ts.TotalDays < 30.4368)//weeks ago
  463. return (int)(ts.TotalDays / 7) == 1 ? "1 Week ago" : (int)(ts.TotalDays / 7) + " Weeks ago";
  464. if (ts.TotalDays < 365.242)//months ago
  465. return (int)(ts.TotalDays / 30.4368) == 1 ? "1 Month ago" : (int)(ts.TotalDays / 30.4368) + " Months ago";
  466. //years ago
  467. return (int)(ts.TotalDays / 365.242) == 1 ? "1 Year ago" : (int)(ts.TotalDays / 365.242) + " Years ago";
  468. }
  469.  
  470. public string RelativeDateTimeCount(DateTime inputDateTime)
  471. {
  472. string outputDateTime = string.Empty;
  473. TimeSpan ts = DateTime.Now - inputDateTime;
  474.  
  475. if (ts.Days > 7)
  476. { outputDateTime = inputDateTime.ToString("MMMM d, yyyy"); }
  477.  
  478. else if (ts.Days > 0)
  479. {
  480. outputDateTime = ts.Days == 1 ? ("about 1 Day ago") : ("about " + ts.Days.ToString() + " Days ago");
  481. }
  482. else if (ts.Hours > 0)
  483. {
  484. outputDateTime = ts.Hours == 1 ? ("an hour ago") : (ts.Hours.ToString() + " hours ago");
  485. }
  486. else if (ts.Minutes > 0)
  487. {
  488. outputDateTime = ts.Minutes == 1 ? ("1 minute ago") : (ts.Minutes.ToString() + " minutes ago");
  489. }
  490. else outputDateTime = "few seconds ago";
  491.  
  492. return outputDateTime;
  493. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement