如何将文本值转换为分钟和秒

时间:2014-08-13 08:27:37

标签: c# asp.net .net

public partial class _Default : System.Web.UI.Page
{
    static int count = 120;

    protected void Page_Load(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        label1.Visible = true;
        label1.Text = count.ToString();

    }
    protected void tmer1_Tick(object sender, EventArgs e)
    {
        label1.Text = (--count).ToString();

        int m = 0;

        if (Convert.ToInt32(label1.Text) <= m)
        {
            count = 120;
            timer1.Enabled = false;
            Response.Redirect("http://www.google.com");
        }
    }

}

这是我的倒计时代码及其完美的工作..

但它显示120,119而不是这个整数值,我想将其转换为像

 2:00 , 1:59 , 1:58 etc..

所以我该怎么做?

2 个答案:

答案 0 :(得分:1)

使用

TimeSpan.FromSeconds(--count).ToString(@"mm\:ss");

也许这可以帮到你

您可以在以下链接中找到更多信息:

TimeSpan.FromSeconds

<强>解决方案

public partial class _Default : System.Web.UI.Page
{
    static int count = 120;

    protected void Page_Load(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        label1.Visible = true;
        label1.Text = TimeSpan.FromSeconds(count).ToString(@"mm\:ss");

    }
    protected void tmer1_Tick(object sender, EventArgs e)
    {
        var c = --count;
        label1.Text = TimeSpan.FromSeconds(c).ToString(@"mm\:ss");

        int m = 0;

        if (c <= m)
        {
            count = 120;
            timer1.Enabled = false;
            Response.Redirect("http://www.google.com");
        }
    }
}

答案 1 :(得分:-1)

            string strrr = "159";
            TimeSpan t=TimeSpan.FromSeconds(Convert.ToDouble(strrr));
            Console.WriteLine(t.ToString());