Get elapsed time from a string

Asked

Viewed 229 times

0

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 longer than our time zone, wanted some kind of exit:

Já se decorreu 0 horas, 0 minutos e 0 segundos...
  • I don’t understand what you want. Is there any specific difficulty or you just don’t know how to convert to string for datetime. Just a detail, this is a common full date, not a elapsed time.

  • I just want to compare the time now Datetime.Now with the one I receive, and return the time elapsed between the 2 as mentioned in the text.

  • Did any of the answers solve your problem? Do you think you can accept one of them? If you haven’t already, see [tour] how to do this. You would help the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site. You can even accept yours.

2 answers

3

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();
        }

It is not the best solution but solved my question.

Browser other questions tagged

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