Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this string: 28 June 2018 (22:05)

How can I compare it with my current time and get the difference?

For example if actual time was 29/06/2018 (05:49)

The difference will be: 7 hours 44 minutes

So input: 28 June 2018 (22:05) Output: 7 hours 44 minutes

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
296 views
Welcome To Ask or Share your Answers For Others

1 Answer

The first thing you need to do, is convert the string to a valid DateTime instance.

If you know your dates will always be in this format, you can do the following...

Dim mydate = DateTime.ParseExact("28 June 2018 (22:05)", "dd MMMM yyyy (HH:mm)", CultureInfo.InvariantCulture)

https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx

Once you've parsed the string into a valid DateTime instance, you can use all the normal date functions to do the comparisons.

I would first get the difference in minutes, like so...

Dim diffminutes = DateDiff(DateInterval.Minute, mydate, Now)

Then create a timespan like this...

Dim mytimespan = TimeSpan.FromMinutes(diffminutes)

Finally display the difference in hours and minutes like this...

Response.Write(mytimespan.ToString("hh:mm"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...