Solved

How do I convert an integer into time string?

Posted on 2013-02-07
5
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …

751 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