Solved

How do I convert an integer into time string?

Posted on 2013-02-07
5
342 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 74

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now