Work with a time span in Javascript

Sounds like you need moment.js e.g. moment().subtract(‘days’, 6).calendar(); => last Sunday at 8:23 PM moment().startOf(‘hour’).fromNow(); => 26 minutes ago Edit: Pure JS date diff calculation: var date1 = new Date(“7/Nov/2012 20:30:00”); var date2 = new Date(“20/Nov/2012 19:15:00”); var diff = date2.getTime() – date1.getTime(); var days = Math.floor(diff / (1000 * 60 * 60 * 24)); … Read more

How convert TimeSpan to 24 hours and minutes String?

myTimeSpan.ToString(@”hh\:mm”) Custom TimeSpan Format Strings The custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals. For example, “dd.hh\:mm” defines a period (.) as … Read more

What is the easiest way to subtract time in C#?

These can all be done with DateTime.Add(TimeSpan) since it supports positive and negative timespans. DateTime original = new DateTime(year, month, day, 8, 0, 0); DateTime updated = original.Add(new TimeSpan(5,0,0)); DateTime original = new DateTime(year, month, day, 17, 0, 0); DateTime updated = original.Add(new TimeSpan(-2,0,0)); DateTime original = new DateTime(year, month, day, 17, 30, 0); DateTime … Read more

Format TimeSpan greater than 24 hour

Well, the simplest thing to do is to format this yourself, e.g. return string.Format(“{0}hr {1}mn {2}sec”, (int) span.TotalHours, span.Minutes, span.Seconds); In VB: Public Shared Function FormatTimeSpan(span As TimeSpan) As String Return String.Format(“{0}hr {1}mn {2}sec”, _ CInt(Math.Truncate(span.TotalHours)), _ span.Minutes, _ span.Seconds) End Function I don’t know whether any of the TimeSpan formatting in .NET 4 would … Read more