Get elapsed time of a string

1

I wanted to calculate the time now with what I get from a string like this:

2016-04-16T15:55:53Z

But the time is three hours higher than our spindle, I wanted some output like:

Já se decorreu 0 horas, 0 minutos e 0 segundos...
    
asked by anonymous 16.04.2016 / 20:09

2 answers

3

I'll answer what you give:

DateTime.Now - DateTime.Parse("2016-04-16T15:55:53Z")

If you're not sure that the text has this format, it's best to use TryParseExact() .

As for the spindle depends on how you have this information, it needs to be somewhere. There are several ways to solve this, some more correct than others depending on the scenario.

To put the extended elapsed time I already answered another question .

    
16.04.2016 / 20:29
0
static void Main(string[] args)
        {
            int hours, minutes, seconds;
            DateTime dt1,dt2;
            dt1 = Convert.ToDateTime("2016-04-16T15:55:53Z");
            dt2 = DateTime.Now;
            dt2.AddHours(3);
            seconds = (int)dt2.Subtract(dt1).TotalSeconds % 60;
            minutes = (int)dt2.Subtract(dt1).TotalMinutes % 60;
            hours = (int)dt2.Subtract(dt1).TotalHours;
            Console.WriteLine("Ja se passaram: " + hours + " hora(s), " + minutes + " minuto(s) e " + seconds + " segundo(s)");
            Console.ReadKey();
        }

Not the best solution but solved my question.

    
16.04.2016 / 20:50