How to get a difference between two dates?

Asked

Viewed 6,519 times

7

Let’s just say I have an initial date equal to 30/01/2014 10:00 and a final date equal to 02/02/2014 10:00.

I would like to know if it is possible to obtain a grid containing these results:

inserir a descrição da imagem aqui

If possible, I would like to know how.

3 answers

3

Use the Datediff() function. For example:

Dim diffDatas as Integer
diffDatas = DateDiff("d", #30/01/2012#, #31/01/2014#)

The first parameter represents which measure you want to calculate the difference, whether in days, whether in months, years, minutes, etc. The second parameter contains the initial date and the third the final date.

You can choose the following intervals:

yyyy - Ano
q    - trimestre (quarter)
m    - Mes
y    - Dia do ano
d    - Dia
w    - Dia da semana
ww   - Semana
h    - Hora
s    - Segundos 

Follow the function reference on the MSDN website: (http://msdn.microsoft.com/en-us/library/b5xbyt6f(v=vs.90). aspx)

  • Actually it is not only a Datediff, but rather show tbm the days that are within that range

  • in this case Voce needs to do a while from the initial date to the final date and save each date in that interval in a Dates array, since the number of days varies according to the initial and final dates.

  • would have an example of what this array would look like ?

0

Hello, one more suggestion.

It consists of a simple class that you can reuse in various parts of your code.

class DiferencaDatas
{
    public DateTime DataInicial { get; set; }
    public DateTime DataFinal { get; set; }

    public DiferencaDatas()
    {
    }

    public DiferencaDatas(DateTime dataInicial, DateTime dataFinal)
    {
        DataInicial = dataInicial;
        DataFinal = dataFinal;
    }

    //As funções retornam a diferença em minutos, horas ou dias.

    //Mostra a diferença de datas entre uma data qualquer e a data atual
    public string DifDataAtual()
    {
        TimeSpan diferenca = DateTime.Now.Subtract(DataInicial);

        if (diferenca.TotalMinutes < 60)
        {
            return $"{diferenca.TotalMinutes.ToString()} Minutos.";
        }
        else if (diferenca.TotalHours < 24)
        {
            return $"{diferenca.TotalHours.ToString("F1", CultureInfo.InvariantCulture)} Horas.";
        }
        else
        {
            return $"{diferenca.TotalDays.ToString("F1", CultureInfo.InvariantCulture)} Dias.";
        }   
    }

    //Mostra a diferfença entre duas datas qualquer
    public string DifDuasDatas()
    {
        TimeSpan diferenca = DataFinal.Subtract(DataInicial);

        if (diferenca.TotalMinutes < 60)
        {
            return $"{diferenca.TotalMinutes.ToString()} Minutos.";
        }
        else if (diferenca.TotalHours < 24)
        {
            return $"{diferenca.TotalHours.ToString("F1", CultureInfo.InvariantCulture)} Horas.";
        }
        else
        {
            return $"{diferenca.TotalDays.ToString("F1", CultureInfo.InvariantCulture)} Dias.";
        }
    }
}

0

Hello

I use it that way

Dim Data_1 As Date = FormatDateTime(DtpData_Inicio.Value)

 Dim Data_2 As Date = FormatDateTime(DtpData_Fim.Value)

'Aqui, um txt traz o resultado em número decimal

TextBox1.Text += (DateDiff(DateInterval.Day, DtpData_Inicio, DtpData_Fim).ToString)

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.