- Home /
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");
}
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.
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");
}
Ah sorry, that should be
System.Timespan timeDifference = datevalue1 - datevalue2;
The formatting options for Timespan are similar to those for DateTime.
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.
it is working .But when the hour actually remain 0:mm:ss,it becomes 12:mm:ss.Can it be solved ? :(
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.
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
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.
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;
}
@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
Follow this Question
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