Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by sdgd · Apr 01, 2013 at 05:00 PM · c#timeformat

how to Format time float (days/hours/minutes/seconds)

I just can't figure it out following

  • this 1

  • this 2

  • this 3

I've done code:

 if (TimeToDisableHelp == 0 ){
     GUILayout.Label("Time Help to Fade is false");
 }
 else if (TimeToDisableHelp < 60){
     GUILayout.Label("Time Help to Fade " + (TimeToDisableHelp % 60).ToString("F0") + "s");
 }
 else if (TimeToDisableHelp < 3600){
     GUILayout.Label("Time Help to Fade " + ((TimeToDisableHelp/60) % 60).ToString("F0") + "min " + (TimeToDisableHelp % 60).ToString("F0") + "s");
 }
 else if (TimeToDisableHelp < 86400){
     GUILayout.Label("Time Help to Fade " + ((TimeToDisableHelp/ 3600) % 24).ToString("F0") + "Hour " + ((TimeToDisableHelp/60) % 60).ToString("F0") + "min " + (TimeToDisableHelp % 60).ToString("F0") + "s");
 }
 
 //                else if (false){
 //                    TimeToDisableHelp = String.Format ("{0:00}:{1:00}:{2:00}:{3:00}",displayDays,displayHours, displayMinutes, displaySeconds); 
 //                }// nothing works here
                 
 TimeToDisableHelp = GUILayout.HorizontalScrollbar(TimeToDisableHelp,0.01f, 0,TimeDisableHelpSliderMax ,GUILayout.Width(50));
 GUILayout.Label("Time help whole number: " + TimeToDisableHelp.ToString("F0") );
                 GUILayout.Label("Bunny's way " + TimeToDisableHelp.ToString("yyyy-MM-dd_HH:mm:ss") );

for my code I get depending on size of TimeToDisableHelp:

Time Help to Fade 2min 30s

Time help whole number: 90

Bunny's way yyyy-MM-dd_HH:mm:ss // that's all nothing changes


but if I have: 89

Time Help to Fade 1min 29s

Time help whole number: 89


but if I have: 5409

Time Help to Fade 2hours 30min 9s

Time help whole number: 5409


but if I have: 5339

Time Help to Fade 1hours 30min 59s

Time help whole number: 5339

I don't have smallest idea what % does and how to solve this.

Comment
Add comment
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

1 Reply

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

Answer by TonyLi · Apr 01, 2013 at 05:43 PM

How about replacing all that with TimeSpan.FromSeconds().ToString()? The line below converts TimeToDisableHelp to a general-format string and assigns it to s.

 string s = TimeSpan.FromSeconds(TimeToDisableHelp).ToString("");

TimeSpan reference: http://msdn.microsoft.com/en-us/library/system.timespan.aspx

ToString() method: http://msdn.microsoft.com/en-us/library/dd992632.aspx

String formats: http://msdn.microsoft.com/en-us/library/ee372286.aspx

Comment
Add comment · Show 7 · 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 sdgd · Apr 01, 2013 at 06:15 PM 0
Share
 Assets/Scripts/$$anonymous$$ain$$anonymous$$enu.cs(382,46): error CS0103: The name `TimeSpan' does not exist in the current context

than I remembered I saw it in 1. one

than I included using system and now:

 Assets/Scripts/$$anonymous$$ain$$anonymous$$enu.cs(383,86): error CS1501: No overload for method `ToString' takes `1' arguments

for

 string str = TimeSpan.FromSeconds(TimeToDisableHelp).ToString("g");

avatar image TonyLi · Apr 01, 2013 at 06:26 PM 0
Share

So does it work if you include the System namespace (using System) or change that line to:

 string str = System.TimeSpan.FromSeconds(TimeToDisableHelp).ToString("g");
avatar image sdgd · Apr 01, 2013 at 06:37 PM 0
Share

no I still get:

 string str = System.TimeSpan.FromSeconds(TimeToDisableHelp).ToString("g");

 Assets/Scripts/$$anonymous$$ain$$anonymous$$enu.cs(395,93): error CS1501: No overload for method `ToString' takes `1' arguments
avatar image TonyLi · Apr 01, 2013 at 11:47 PM 0
Share

Sorry, I wasn't near Unity to test. The version of ToString() that takes arguments is .NET 4.0, and Unity is essentially .NET 3.5, so you can only call ToString() without arguments. It returns a string of the form:

 [-][d.]hh:mm:ss[.fffffff]

where the parts in square brackets will only appear if necessary. So you'll most likely get something of the form hh:mm:ss.

Here's the reference page for the .NET 3.5 version: http://msdn.microsoft.com/en-us/library/vstudio/1ecy8h51(v=vs.90).aspx

avatar image sdgd · Apr 02, 2013 at 07:57 AM 0
Share

ok got it now but I don't know where to put this

 [-][d.]hh:mm:ss[.fffffff]

or how to get rid of 0.000000 sec

or how to make ins$$anonymous$$d

  ":" to "$$anonymous$$" or "hours"


can you edit your answer and remove that "g" ?

Show more comments

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image 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

Multiple Cars not working 1 Answer

String format to show float as time 3 Answers

Distribute terrain in zones 3 Answers

Fade in AudioLister over time #c 1 Answer


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