Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert int 90 minutes to DateTime 1:30?

How can I convert an int 90, for example, to DateTime 1:30 in C# 3.0?

Thanks!!

like image 513
user95542 Avatar asked May 21 '09 18:05

user95542


People also ask

How do you convert time to Date and time?

Extract time only from datetime with formula 1. Select a blank cell, and type this formula =TIME(HOUR(A1),MINUTE(A1), SECOND(A1)) (A1 is the first cell of the list you want to extract time from), press Enter button and drag the fill handle to fill range.

How to convert DateTime to minutes in c#?

If you subtract one DateTime from another, you'll get a TimeSpan result automatically. It has members that can give you total minutes or seconds or hours, etc., or the individual parts.

How do you convert int to time in python?

Use pandas. to_datetime() to Convert Integer to Date & Time Format. Let's suppose that your integers contain both the date and time. In that case, the format should be specify is '%Y%m%d%H%M%S' .


3 Answers

You shouldn't use a DateTime to represent a span of time - use TimeSpan for that. And in such a case, you'd use this:

TimeSpan ts = TimeSpan.FromMinutes(90);

If you insist that you need a DateTime, you could do the following:

DateTime dt = DateTime.Now.Date; // To get Midnight Today
dt = dt.AddMinutes(90); // to get 90-minutes past midnight Today.

The reason you probably don't want to use DateTime, though, is that it (aptly named) combines the concept of Date with the concept of Time. Your question suggests that you're planning to ignore the date component, so in the interests of using the right tool for the job, I suggest TimeSpan.

like image 64
Erik Forbes Avatar answered Sep 28 '22 12:09

Erik Forbes



If you use C# 4 (VS 2010) you need this snippet:

TimeSpan ts = TimeSpan.FromSeconds(90);
txtDate = string.Format("Full time: {0}", new DateTime(ts.Ticks).ToString("HH:mm:ss"));

it output

Full time: 00:01:30
like image 25
elp Avatar answered Sep 28 '22 12:09

elp


Or if you're trying to add time to a DateTime with just a date:

DateTime dt = dateTime.AddMinutes(90);
like image 1
James L Avatar answered Sep 25 '22 12:09

James L