/// <summary> /// Returns a humanized date time string if the <see cref="DateTimeOffset"/> falls within /// the given <paramref name="cutoffDays"/>, otherwise returns a formatted date time string. /// </summary> /// <param name="dt">The <see cref="DateTimeOffset"/>.</param> /// <param name="timeZoneId">User local timezone.</param> /// <param name="cutoffDays">Default 2.</param> /// <param name="format">Default "yyyy-MM-dd".</param> /// <returns></returns> public static string ToDisplayString(this DateTimeOffset dt, string timeZoneId, int cutoffDays = 2, string format = "yyyy-MM-dd") { return((DateTimeOffset.UtcNow.Day - dt.Day) > cutoffDays? dt.ToLocalTime(timeZoneId).ToString(format) : dt.ToLocalTime(timeZoneId).Humanize()); }
static DateTimeOffset GetDateTimeOffset(long ticks, bool convertLocalTime = true) { var dt = new DateTimeOffset(ticks, TimeSpan.Zero); return(convertLocalTime ? dt.ToLocalTime() : dt); }