Solved

How do I convert an integer into time string?

Posted on 2013-02-07
5
352 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:mikesExpertExchange
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 500 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:mikesExpertExchange
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:mikesExpertExchange
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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