Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Faisalalamapu · May 28, 2016 at 05:08 AM · c#scripting beginnertimertimer countdown

make a countdown timer

I am making a application using unity3d. In a scene ,I want to show a countdown to the users. But the countdown should stop at 6 pm (20-06-2016) .I have found a script where I can get the time difference between current time and the date I mentioned .But I can not set the time(6pm) in the date. Is there any way to get difference between current time and (6pm 20-06-2016)? and the remaining time should be in this format : hr:mm:ss .thanks in advance :)

 void Start () {
         System.DateTime datevalue1 = new System.DateTime(2016,06,07 );
         System.DateTime datevalue2 = System.DateTime.Now;
         double hours = (datevalue1 - datevalue2).TotalHours;
         
         Debug.Log ("It has been " + hours.ToString() + " hours since the beginning of the year");
     }
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
3
Best Answer

Answer by cjdev · May 28, 2016 at 07:18 AM

Try using the DateTime constructor with 6 arguments: new DateTime(year, month, day, hour, minute, second). For the formatting try using the ToString options:

 System.DateTime timeDifference = datevalue1 - datevalue2;
 string time = timeDifference.ToString("H:mm:ss");

See here for more formatting options.

Comment
Add comment · Show 12 · 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 Faisalalamapu · May 28, 2016 at 07:59 AM 0
Share

When I try this ,I got this error :( How can I solve it? Cannot implicitly convert type System.TimeSpan' to System.DateTime' void Update () {

         System.DateTime datevalue1 = new System.DateTime(2016,05,28,16,00,00 );
         System.DateTime datevalue2 = System.DateTime.Now;
 
 
         System.DateTime timeDifference = datevalue1 - datevalue2;
         string time = timeDifference.ToString("H:mm:ss");
         }
 
avatar image cjdev Faisalalamapu · May 28, 2016 at 08:09 AM 0
Share

Ah sorry, that should be

 System.Timespan timeDifference = datevalue1 - datevalue2;

The formatting options for Timespan are similar to those for DateTime.

avatar image cjdev cjdev · May 28, 2016 at 08:49 AM 1
Share

Alright this code works:

 System.DateTime datevalue1 = new System.DateTime(2016,06,07,0, 0, 0 );
 System.DateTime datevalue2 = System.DateTime.Now;
 System.TimeSpan timeDifference = datevalue1 - datevalue2;
 string time = new System.DateTime(timeDifference.Ticks).ToString("dd:hh:mm:ss");
 Debug.Log(time);

I didn't capitalize the S in TimeSpan before, that's what I get for not testing the code. Sorry about that.

Show more comments
avatar image Faisalalamapu · May 28, 2016 at 02:54 PM 0
Share

it is working .But when the hour actually remain 0:mm:ss,it becomes 12:mm:ss.Can it be solved ? :(

avatar image cjdev Faisalalamapu · May 28, 2016 at 03:11 PM 0
Share

Are you using the first format string? I think the capital H is for a 24 hour format, try using the "dd:hh:mm:ss" type ins$$anonymous$$d.

avatar image Faisalalamapu cjdev · May 28, 2016 at 03:29 PM 0
Share

oh thanks ,HH:mm:ss is working perfectly .

Show more comments
avatar image Faisalalamapu · May 28, 2016 at 04:24 PM 0
Share

well I want to fill a circle loading bar depending on this timer.is it possible? Here is the full script.I am getting this errors :( Cannot implicitly convert type System.DateTime' to float' Operator /' cannot be applied to operands of type float' and `System.TimeSpan using UnityEngine; using System.Collections; using UnityEngine.UI; using System;

 public class loading : $$anonymous$$onoBehaviour {
 
     public Transform LoadingBar;
     public Transform TextIndicator;
     public Transform TextLoading;
     [SerializeField] private float CurrentAmmount;
     [SerializeField] private float speed;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         System.DateTime datevalue1 = new System.DateTime(2016,05,28,22, 00, 00 );
         System.DateTime datevalue2 = System.DateTime.Now;
         System.TimeSpan timeDifference = datevalue1 - datevalue2;
         string time = new System.DateTime(timeDifference.Ticks).ToString("HH:mm:ss");
         CurrentAmmount = datevalue2;
 
         if (CurrentAmmount < 100) {
             CurrentAmmount += speed * Time.deltaTime;
             TextIndicator.GetComponent<Text>().text= time.ToString();
             TextLoading.gameObject.SetActive(true);
         } else {
             TextLoading.gameObject.SetActive(false);
             TextIndicator.GetComponent<Text>().text = "Done!";
         }
     
         LoadingBar.GetComponent<Image> ().fillAmount = CurrentAmmount /timeDifference;
     }
 
 }
 And here is snapshot,what I am making

alt text

capture.jpg (22.6 kB)
avatar image cjdev Faisalalamapu · May 28, 2016 at 04:38 PM 0
Share

You can't compare TimeSpan and DateTime to numbers, they're objects. I'd recommend using timeDifference.Ticks to keep track of the total amount and then start the current amount at 0.

avatar image Faisalalamapu cjdev · May 28, 2016 at 05:08 PM 0
Share

I coded like this .There is no error but the circle is not filling :(

 void Update ()
 if (CurrentAmmount < timeDifference.Ticks) {
             CurrentAmmount += speed * Time.deltaTime;
             TextIndicator.GetComponent<Text>().text= time.ToString();
             TextLoading.gameObject.SetActive(true);
         } else {
             TextLoading.gameObject.SetActive(false);
             TextIndicator.GetComponent<Text>().text = "Done!";
         }
     
         LoadingBar.GetComponent<Image> ().fillAmount = CurrentAmmount /timeDifference.Ticks;
 
     }
 

avatar image Ashleigh_ · Mar 24, 2021 at 07:04 AM 0
Share

@Faisalalamapu Hi, did you end up solving this? I'm trying to do the same thing, I would really appreciate seeing your code if you did please

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

timer not ticking down 2 Answers

How do I make the timeScale not affect the timer? 1 Answer

Player won't respawn after timer hits zero. 2 Answers

CountDownTimer Help 1 Answer

Cant Clamp 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