I need to format the date for Apr 06, 2018 . Code that I have:
DateTime joinDate = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Group.GetGroupJoinInfo(Group.Id));
base.WriteString(joinDate.ToString("dd/MM/yyyy"));
I need to format the date for Apr 06, 2018 . Code that I have:
DateTime joinDate = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Group.GetGroupJoinInfo(Group.Id));
base.WriteString(joinDate.ToString("dd/MM/yyyy"));
First, create an object DateTime
with the desired date:
DateTime date1 = DateTime.ParseExact("06/04/2018", "dd/MM/yyyy", CultureInfo.InvariantCulture);
Then convert it to the desired format:
string resultDate = date1.ToString("MMM dd, yyyy");
The conversion may generate an exception if it is not in the correct format. For this I have improved the code for the following.
try
{
if (!DateTime.TryParseExact("06/04/2018", "dd/MM/yyyy", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime date1))
{
throw new FormatException("Formato de data inválido");
}
string resultDate = date1.ToString("MMM dd, yyyy");
}
catch(Exception ex)
{
// Tratar exceção gerada, seja mostrando uma mensagem ou registrando em um arquivo de log
}