Solved

How do I convert an integer into time string?

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

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

14 Experts available now in Live!

Get 1:1 Help Now