datediff
Calculate exact date difference in years using SQL
All datediff() does is compute the number of period boundaries crossed between two dates. For instance datediff(yy,’31 Dec 2013′,’1 Jan 2014′) returns 1. You’ll get a more accurate result if you compute the difference between the two dates in days and divide by the mean length of a calendar year in days over a 400 … Read more
Twig date difference
Since PHP 5.3 There is another option without to write an extension. This example show how to calc the plural day/days {# endDate and startDate are strings or DateTime objects #} {% set difference = date(endDate).diff(date(startDate)) %} {% set leftDays = difference.days %} {% if leftDays == 1 %} 1 day {% else %} {{ … Read more
How to convert number of minutes to hh:mm format in TSQL?
You can convert the duration to a date and then format it: DECLARE @FirstDate datetime, @LastDate datetime SELECT @FirstDate=”2000-01-01 09:00:00″, @LastDate=”2000-01-01 11:30:00″ SELECT CONVERT(varchar(12), DATEADD(minute, DATEDIFF(minute, @FirstDate, @LastDate), 0), 114) /* Results: 02:30:00:000 */ For less precision, modify the size of the varchar: SELECT CONVERT(varchar(5), DATEADD(minute, DATEDIFF(minute, @FirstDate, @LastDate), 0), 114) /* Results: 02:30 */
Count days between two dates, excluding weekends (MySQL only)
Simply try it using a simple function : CREATE FUNCTION TOTAL_WEEKDAYS(date1 DATE, date2 DATE) RETURNS INT RETURN ABS(DATEDIFF(date2, date1)) + 1 – ABS(DATEDIFF(ADDDATE(date2, INTERVAL 1 – DAYOFWEEK(date2) DAY), ADDDATE(date1, INTERVAL 1 – DAYOFWEEK(date1) DAY))) / 7 * 2 – (DAYOFWEEK(IF(date1 < date2, date1, date2)) = 1) – (DAYOFWEEK(IF(date1 > date2, date1, date2)) = 7); Test … Read more
DATEDIFF function in Oracle [duplicate]
In Oracle, you can simply subtract two dates and get the difference in days. Also note that unlike SQL Server or MySQL, in Oracle you cannot perform a select statement without a from clause. One way around this is to use the builtin dummy table, dual: SELECT TO_DATE(‘2000-01-02’, ‘YYYY-MM-DD’) – TO_DATE(‘2000-01-01’, ‘YYYY-MM-DD’) AS DateDiff FROM … Read more
Count number of Mondays in a given date range
Try this: static int CountDays(DayOfWeek day, DateTime start, DateTime end) { TimeSpan ts = end – start; // Total duration int count = (int)Math.Floor(ts.TotalDays / 7); // Number of whole weeks int remainder = (int)(ts.TotalDays % 7); // Number of remaining days int sinceLastDay = (int)(end.DayOfWeek – day); // Number of days since last [day] … Read more
Date difference in years using C# [duplicate]
I have written an implementation that properly works with dates exactly one year apart. However, it does not gracefully handle negative timespans, unlike the other algorithm. It also doesn’t use its own date arithmetic, instead relying upon the standard library for that. So without further ado, here is the code: DateTime zeroTime = new DateTime(1, … Read more
How to compare two dates to find time difference in SQL Server 2005, date manipulation
Take a look at the DateDiff() function. — Syntax — DATEDIFF ( datepart , startdate , enddate ) — Example usage SELECT DATEDIFF(DAY, GETDATE(), GETDATE() + 1) AS DayDiff SELECT DATEDIFF(MINUTE, GETDATE(), GETDATE() + 1) AS MinuteDiff SELECT DATEDIFF(SECOND, GETDATE(), GETDATE() + 1) AS SecondDiff SELECT DATEDIFF(WEEK, GETDATE(), GETDATE() + 1) AS WeekDiff SELECT DATEDIFF(HOUR, … Read more
How to get difference between two dates in months using MySQL query?
Month-difference between any given two dates: I’m surprised this hasn’t been mentioned yet: Have a look at the TIMESTAMPDIFF() function in MySQL. What this allows you to do is pass in two TIMESTAMP or DATETIME values (or even DATE as MySQL will auto-convert) as well as the unit of time you want to base your … Read more