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
2
Question by Ishkur · Jul 20, 2012 at 10:29 AM · formatdatetimetostringtimespan

How to format TimeSpan.ToString in .NET 4.0

Hello, I'm trying to format my TimeSpan string using

 myString = myTimeSpan.ToString("c");

However the TimeSpan.ToString method does not take a format string as an argument until .NET 4.0 while Unity's version of Mono is still on 3.5.

How would you format DateTime and TimeSpan strings then?

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
3
Best Answer

Answer by whydoidoit · Jul 20, 2012 at 10:30 AM

myString = string.Format("{0:c}", myTimeSpan);

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 Ishkur · Jul 20, 2012 at 11:19 AM 0
Share

Thank you! However this solution works only for formatting DateTime.

In the case of TimeSpan, even if the format string is changed such as

myString = string.Format("{0:%s}", myTimeSpan);

which should only output the value of the myTimeSpan.Seconds property, myTimeSpan is still displayed with the default format string "c", showing dd:hh:mm:ss:fffffff.

So it doesn't seem to work for formatting TimeSpan.

avatar image whydoidoit · Jul 20, 2012 at 02:00 PM 0
Share

So what format are you after? Something like this:

   myString = string.Format("{0:00}:{1:00}", myTimeSpan.$$anonymous$$inutes, myTimeSpan.Seconds);
avatar image Ishkur · Jul 21, 2012 at 03:32 AM 0
Share

Yes! This is what I was looking for!

I had been using a workaround as such:

myString = string.Format("{0:HH\\:mm\\:ss}", new DateTime(myTimeSpan.Ticks));

but your solution is much more correct, thank you.

avatar image
-2

Answer by mindlube · Jul 21, 2012 at 03:39 AM

I would ask on stackoverflow

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 Ash-Blue · Apr 16, 2017 at 12:16 PM 0
Share

Stackoverflow is a great resource, but I personally find that Unity-centric questions get better answers here. Also this question is for an older version of C# and Stackoverflow tends to focus on the latest specification from my experience.

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

Formatting Date and Time 3 Answers

Export objects to a .3DS file at runtime 1 Answer

System.DateTime truncate Milliseconds 1 Answer

Help, getting ambiguous reference error 1 Answer

Help, getting ambiguous reference error 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