Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I convert an integer into time string?

Posted on 2013-02-07
5
Medium Priority
?
359 Views
Last Modified: 2013-02-07
I'm looking for a clear, simple solution to take and integer and turn it into time. i.e. 7 = "0:07"; 61 = "1:01" etc. Just looking for some sample code to do this.
0
Comment
Question by:Michael Sterling
5 Comments
 
LVL 6

Expert Comment

by:RustyZ32
ID: 38865844
int timeNumber = 61;
DateTime time = DateTime.ParseExact(timeNumber.ToString().PadLeft(6, '0'), "HHmmss", null);
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38865878
http://msdn.microsoft.com/en-us/library/system.timespan.fromminutes.aspx

static void Main(string[] args)
{
    int x = 7;
    TimeSpan t = TimeSpan.FromMinutes(x);

    Console.WriteLine(t.ToString());

    x = 61;
    t = TimeSpan.FromMinutes(x);

    Console.WriteLine(t.ToString());
}

Open in new window

0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 38865886
@ rusty: When i try to assign to the datetime variable i get:

The DateTime represented by the string is not supported in calendar Systme.Globalization.GregorianCalendar."

???
0
 
LVL 1

Author Closing Comment

by:Michael Sterling
ID: 38865907
that worked! thank you!
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38865924
hi ...simply
Private Function MinutesToTime(ByVal dMinutes As Double) _
As String
        MinutesToTime = Format(DateAdd("n", dMinutes, "00:00"), "HH:mm")
    End Function

Open in new window

65 -> 1:05
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question