Obtener período entre dos fechas (años, meses, dias, etc.).

public struct DateTimeSpan
{
private readonly int years;
private readonly int months;
private readonly int days;
private readonly int hours;
private readonly int minutes;
private readonly int seconds;
private readonly int milliseconds;

public DateTimeSpan(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)
{
this.years = years;
this.months = months;
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.milliseconds = milliseconds;
}

public int Years { get { return years; } }
public int Months { get { return months; } }
public int Days { get { return days; } }
public int Hours { get { return hours; } }
public int Minutes { get { return minutes; } }
public int Seconds { get { return seconds; } }
public int Milliseconds { get { return milliseconds; } }

enum Phase { Years, Months, Days, Done }

public static DateTimeSpan CompareDates(DateTime date1, DateTime date2)
{
if (date2 date2)
{
phase = Phase.Months;
current = current.AddYears(years);
}
else
{
years++;
}
break;
case Phase.Months:
if (current.AddMonths(months + 1) > date2)
{
phase = Phase.Days;
current = current.AddMonths(months);
}
else
{
months++;
}
break;
case Phase.Days:
if (current.AddDays(days + 1) > date2)
{
current = current.AddDays(days);
var timespan = date2 – current;
span = new DateTimeSpan(years, months, days, timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds);
phase = Phase.Done;
}
else
{
days++;
}
break;
}
}

return span;
}
}

Ejemplo:
var dateSpan = DateTimeSpan.CompareDates(cpoSIU_ComprobantePagoCab.CpdFechaComprobanteHasta, cpoSIU_ComprobantePagoCab.CpdFechaComprobante);

Funcion para el ingreso de sólo numeros en un control TextBox

private void txtNroTicket_KeyPress(object sender, KeyPressEventArgs e)
{
    //Para obligar a que sólo se introduzcan números 
    if (Char.IsDigit(e.KeyChar))
    {
        e.Handled = false;
    }
    else
    {
        if (Char.IsControl(e.KeyChar)) //permitir teclas de control como retroceso 
        {
            e.Handled = false;
        }
        else
        {
            //el resto de teclas pulsadas se desactivan 
            e.Handled = true;
        }
    }
}

Diferencia entre fechas (sin fines de semana)

Opción 1

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[WorkDayDiff]’) and xtype in (N’FN’, N’IF’, N’TF’))
drop function [dbo].[WorkDayDiff]
GO

CREATE FUNCTION WorkDayDiff(@BegDate datetime,@EndDate datetime)
RETURNS int
BEGIN

DECLARE @NumWeek int
SET @BegDate = IsNull(@BegDate,0)
SET @EndDate = IsNull(@EndDate,0)
SET @NumWeek = DateDiff(ww,@BegDate,@EndDate)
RETURN(datediff(dd,@BegDate,@EndDate)-(@NumWeek*2))
END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

———————-

Opción 2

DECLARE @t TABLE(id INT,StartDt DATETIME,EndDt DATETIME);
INSERT INTO @t VALUES(1,'20100728 13:00', '20100729 10:00');
INSERT INTO @t VALUES(2,'20100728 13:00', '20100729 17:00');

DECLARE @Hrs TABLE(Id INT,DayStart DATETIME, DayEnd DATETIME);
INSERT INTO @Hrs VALUES(1,'19000101 07:00','19000101 17:30');
INSERT INTO @Hrs VALUES(2,'19000101 08:00','19000101 17:00');

SELECT 
 CASE 
  WHEN DATEADD(DAY,DATEDIFF(DAY,0,StartDt),0) < DATEADD(DAY,DATEDIFF(DAY,0,EndDt),0)
  THEN 
   DATEDIFF(MINUTE,DATEADD(DAY,-DATEDIFF(DAY,0,StartDt),StartDt),DayEnd) +
   (DATEDIFF(DAY,StartDt,EndDt)-1) * 1440 +
   DATEDIFF(MINUTE,[DayStart],DATEADD(DAY,-DATEDIFF(DAY,0,EndDt),EndDt))
 ELSE
  DATEDIFF(MINUTE,StartDt,EndDt)
 END / 60.0
FROM @t t
INNER JOIN @Hrs hrs ON t.Id = hrs.Id