Convert string to Time

5

I have a column in the database that receives a type varchar that corresponds to the hours in my application ( "HH:MM" ).

I need to convert this string to a type TIME and then concatenate it with a type DateTime . Has anyone done anything similar?

    
asked by anonymous 14.04.2014 / 16:52

4 answers

4

I would transform the string to TimeSpan and then add it to the DateTime that already exists:

var dateTime = new DateTime(2014, 04, 14);
var finalDateTime = dateTime + TimeSpan.Parse("12:10");

If you are using LINQ to Entities, you could try to bring the data from the already converted database to TimeSpan by using the method: EntityFunctions.CreateTime :

var itens = db.MeuTipo.Select(x => EntityFunctions.CreateTime(
                                       Convert.ToInt32(x.CampoVarChar.Substring(0, 2)),
                                       Convert.ToInt32(x.CampoVarChar.Substring(3, 2)),
                                       null)).ToList();

I just can not test it right now.

    
14.04.2014 / 17:11
2

Problems converting string to DateTime

First, you need to keep in mind that there are several ways a DateTime / TimeSpan can present itself, you can check this here . So it is not very safe for you to convert a string to DateTime / TimeSpan without proper care as this may suffer interference from the regional settings that the server has, since the default DateTime / TimeSpan format is defined by this.

A secure solution

The TryParseExact method allows you to define one or more formats for TimeSpan and DateTime, as well as ignoring the regional language settings and letting us know if it was successful or not! This all guarantees a much more robust code.

Note: Note that I'm not thinking about setting CultureInfo in Web.Config, I just thought of a code-level solution.

The Code

    private static void Main(string[] args)
    {
        DateTime dataJaConhecida = DateTime.Now.Date;

        TimeSpan horasConvertidas;
        if (!TimeSpan.TryParseExact("03:12", @"h\:m", CultureInfo.InvariantCulture, out horasConvertidas))
        {
            Console.WriteLine("Horas no formato inválido");
        }
        else
        {

            Console.WriteLine(dataJaConhecida.ToString("dd/MM/yyyy HH:mm"));

            dataJaConhecida += horasConvertidas;

            Console.WriteLine(dataJaConhecida.ToString("dd/MM/yyyy HH:mm"));
        }

        Console.ReadKey();

    }
    
14.04.2014 / 17:25
0

To do this routine you could simply

DateTime Data = DateTime.Parse("01/01/1999" + " " + "03:03:03");
    
14.04.2014 / 17:00
0

I was able to resolve the case with the following solution:

var horaRetorno = retornoVeiculo.HoraDaChegada.Split(':')[0];
var minRetorno = retornoVeiculo.HoraDaChegada.Split(':')[1];
var tR = retornoVeiculo.DataDaChegada.AddHours(double.Parse(horaR));
tR = tR.AddMinutes(double.Parse(minR));

In this way the string containing the time is broken into 2 parts and at the end both are incorporated into the Time field of type Datetime .

    
16.04.2014 / 16:57