Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by Grimmy · Aug 22, 2010 at 09:42 AM · timestringformat

How do I format a string into days:minutes:hours:seconds?

I know that I need to use String.Format but there is very little explanation of its usage in the mono docs. So far I have..

 timeText = String.Format ("{0:00}:{0:00}:{1:00}:{0:00}",displayDays,displayHours, displayMinutes, displaySeconds);

..which does nothing as I have no idea what the 1's and 0's represent. Can anyone shed some light on this?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Structed · Oct 10, 2013 at 10:13 AM 0
Share

the numbers before the colon represent the index of the objects after the formatting strin. SO in your example, 0 is displayDays, 1 is displayHours and 2 would be displaySeconds.

6 Replies

· Add your reply
  • Sort: 
avatar image
24
Best Answer

Answer by robhuhn · Jun 21, 2012 at 12:53 PM

I prefer using TimeSpan (namespace System) in c#. It's short and simple:

 TimeSpan timeSpan = TimeSpan.FromSeconds(time);
 string timeText = string.Format("{0:D2}:{1:D2}:{2:D2}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Thom Denick · Dec 29, 2012 at 10:31 PM 1
Share

Just a note, TimeSpan is in the System namespace, so you need to add "using System;" to your header.

avatar image Seedersj · Feb 28, 2019 at 09:24 PM -1
Share

This answer is awful. You do not explain what the heck {0:d2} means. It may be "short and simple" when you understand it, but looking at {0:d2} really makes no sense by itself. Telling people it's easy doesn't actually help them understand.

avatar image Hellium Seedersj · Feb 28, 2019 at 11:49 PM 0
Share

Is it this hard to read the manual?

  • https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.7.2#the-format-item

  • https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings?view=netframework-4.7.2

avatar image
6

Answer by Grimmy · Aug 22, 2010 at 10:06 AM

Well I figured out that the first zero is the order of the string array..ie it should read

currentCaseTimeText = String.Format ("{0:00}:{1:00}:{2:00}:{3:00}",displayDays,displayHours, displayMinutes, displaySeconds); 

And that gives me 00:00:00:00 with the correct vars assigned to the correctly formatted areas.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Edy · Aug 22, 2010 at 11:05 AM 1
Share

Right, the first zero is the order of the variable to show. The 00 means the $$anonymous$$imum number of digits to use for displaying that value. Two zeros tell the parser to display the single-digit values (1, 2, 3...) with a leading 0: 01,02,03. Using 000 would lead to 001, 002, 003.

Also, if your values are float, you can specify the number of decimal digits to use by adding a dot and the desired number of decimals as zeros: {0:00.000} --> 03.234

avatar image Cyb3rManiak · Aug 22, 2010 at 11:23 AM 0
Share

You can also use the String.padLeft() and String.PadRight() methods. strBlah.ToString().PadLeft(2, '0'); Plus, if you want to search for info - you can use $$anonymous$$SDN since the methods in $$anonymous$$ono and in .NET are the same. The implementation might be a bit different, but they are compatible and implement the same specs.

avatar image
2

Answer by Guhanesh · Nov 29, 2016 at 02:48 PM

System.DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xonatron · Nov 07, 2017 at 11:46 PM 0
Share

Works! How do you show am/pm?

avatar image Xonatron · Nov 07, 2017 at 11:48 PM 0
Share

Found it. "tt" returns am/pm.

avatar image
1

Answer by yoyo · Dec 03, 2010 at 05:58 PM

Here's the bit of code I'm using to display floating point seconds as MM:SS.FF ...

string FormatSeconds(float elapsed)
{
    int d = (int)(elapsed * 100.0f);
    int minutes = d / (60 * 100);
    int seconds = (d % (60 * 100)) / 100;
    int hundredths = d % 100;
    return String.Format("{0:00}:{1:00}.{2:00}", minutes, seconds, hundredths);
}

(EDIT) Recently realized I can simply use System.DateTime, as demonstrated in this javascript fragment:

var date : System.DateTime;
date = new System.DateTime(seconds * System.TimeSpan.TicksPerSecond);

If you want it as a nicely formatted string, use date.ToString(). This can also take a format parameter to control the formatting details, see MSDN docs for more info.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by yoyo · Dec 03, 2010 at 05:50 PM

If you have a DateTime you can use custom format strings as explained here ... http://social.msdn.microsoft.com/Search/en-us?query=custom+datetime+format+strings

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xonatron · Nov 07, 2017 at 11:49 PM 0
Share

The first result from this search helped me: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to convert timer to minutes, seconds and cents 3 Answers

String format to show float as time 3 Answers

Split string from PlayePrefs 1 Answer

FormatException: Input string was not in the correct format 1 Answer

Formatting Date and Time 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges