DateTime Extensions
[EN] In this extension, calculates the difference in days between dates. It is possible to ignore weekends in the count.
[PT-BR] Nesta extensão, calcula a diferença de dias entre as datas. É possível ignorar os finais de semana na contagem.
public void test()
{
var initialDate = new DateTime(2022, 1, 1);
var endDate = new DateTime(2022, 1, 20);
Console.WriteLine(initialDate.DifferenceInDaysBetweenDates(endDate, false));
// 19
}
[EN] In this extension, checks if the date is weekend.
[PT-BR] Nesta extensão, verifica se a data é final de semana.
public void test()
{
var dt = new DateTime(2022, 1, 29);
if (dt.IsWeekend())
{
// TODO
}
else
{
// TODO
}
}
[EN] In this extension, returns the first day of the month.
[PT-BR] Nesta extensão, retorna o primeiro dia do mês.
public void test()
{
var dt = new DateTime(2022, 1, 29);
Console.WriteLine(dt.FirstDayOfMonth());
// 2022-01-01T00:00:00
}
[EN] In this extension, returns the first hour of the day.
[PT-BR] Nesta extensão, retorna a primeira hora do dia.
public void test()
{
var dt = new DateTime(2022, 1, 29, 15, 15, 15);
Console.WriteLine(dt.FirstTimeOfDay());
// 2022-01-29T00:00:00
}
[EN] In this extension, returns the last day of the month.
[PT-BR] Nesta extensão, retorna o último dia do mês.
public void test()
{
var dt = new DateTime(2022, 1, 29);
Console.WriteLine(dt.LastDayOfMonth());
// 2022-01-31T00:00:00
}
[EN] In this extension, returns the last hour of the day.
[PT-BR] Nesta extensão, retorna a última hora do dia.
public void test()
{
var dt = new DateTime(2022, 1, 29, 15, 15, 15);
Console.WriteLine(dt.LastTimeOfDay());
// 2022-01-29T23:59:59
}
[EN] In this extension, returns the next business day. It is possible to pass a list of daysoff to be discarded.
[PT-BR] Nesta extensão, retorna o próximo dia útil. É possível passar uma lista de feriados para serem descartados.
public void test()
{
var dt = new DateTime(2022, 1, 29);
var daysOff = new DateTime[]
{
new DateTime(2022, 1, 31)
};
Console.WriteLine(dt.NextBusinessDay(daysOff));
// 2022-02-01T00:00:00
}
[EN] In this extension, calculate Age.
[PT-BR] Nesta extensão, calcula a idade.
public void test()
{
var dt = new DateTime(1955, 10, 28);
Console.WriteLine(dt.YearsOld());
// 66
}