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 Numli · Jul 26, 2014 at 07:49 PM · timespan

Formatting a DateTime.TimeSpan

Hi All, I am making a life timer that gives the user a life back after 15 minutes and so far the system is working well, I am now trying to display the time until a new life is given, I have saved the time the life will be given in PlayerPrefs and that works fine, also TimeSpan is almost a perfect solution apart from the fact that I am struggling to format it to only show the minutes:Seconds left not the days/hours/mins/sec/millisec is there anyway to format TimeSpan? I have looked through this site and also googled it but I cannot find a solution that actually works. My Code is as follows for getting the time span and I just use that in a GUI.Label

 DateTime.TryParse(PlayerPrefs.GetString("Life1"), life1);
     nowTime = DateTime.UtcNow;
     timeleft = life1 - nowTime;


Sorry if this has been asked before and I have just missed the answer. I am new to unity so sorry if its a simple question!

Thanks in advance :)

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

2 Replies

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

Answer by MaT227 · Jul 26, 2014 at 09:07 PM

Here is a link that might help you to format TimeSpan : Standard TimeSpan Format Strings

And here is the complete list of the formatting possibilties of TimeSpan : Custom TimeSpan Format Strings

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 Numli · Jul 27, 2014 at 12:31 PM 0
Share

Thank you very much for the help, I looked through the links you sent however I just kept on getting the error "No appropriate version of 'System.TimeSpan.ToString' for the argument list '(String)' was found." It did however give me the idea to approach it like this which seems to work but maybe not in the most optimal fashion, I am using unity script, im not sure if that is why it is returning an error? This is what I have ended up with:

 timeleft = life1 - nowTime;
     var formattedTimeLeft0 = timeleft.ToString();
     var splitTimeFull = formattedTimeLeft0.Split(":"[0]);
     var splitTime$$anonymous$$illi = splitTimeFull[2].Split("."[0]);
     formattedTimeLeft = splitTimeFull[1] + ":" + splitTime$$anonymous$$illi[0];

Again thank you very much for the help.

avatar image MaT227 · Jul 28, 2014 at 08:16 AM 0
Share

Unfortunately I am not used to UnityScript and I can't help you further. :( If you found the solution yourself, I suggest you to post it as an answer and validate it and thumb up for helping answers.

avatar image
0

Answer by BananaAcid · Oct 22, 2017 at 11:51 PM

just for anyone looking for something similar: i went with

 private string toStr(TimeSpan t)
 {
     return t.Hours + ":" + t.Minutes + ":" + t.Seconds + "." + t.Milliseconds;
 }
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Subtract milliseconds from 12 hours? 1 Answer

Leader board which maintains different time zones ? 2 Answers

Serializing a Timespan 1 Answer

Best Practices: TimeSpans 1 Answer

Timespan to int total seconds 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