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...
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...
3
I’ll answer the question:
DateTime.Now - DateTime.Parse("2016-04-16T15:55:53Z")
I put in the Github for future reference.
If you are not sure that the text has this format it is best to use the TryParseExact()
.
As for the time zone depends on how you have that information, you need to be somewhere. There are several ways to solve this, some more correct than others depending on the scenario.
To put the time elapsed in full I already answered in another question.
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 c# .net json string datetime
You are not signed in. Login or sign up in order to post.
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.– Maniero
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.
– Marcos Barbosa
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.
– Maniero