如何从“DateTime”值中减去一定的分钟数?

时间:2016-10-13 13:08:43

标签: c# .net datetime

在下面的代码中,我想从datetime.now().tostring()中减去5分钟作为整数I,而不是当前系统时间。因此,警告可以在系统时间与labeltext1中的时间匹配前5分钟播放。

private void timer1_Tick(object sender, EventArgs e)
{
    label_time1.Text = DateTime.Now.ToString();
    mplayer = new SoundPlayer();
    mplayer.SoundLocation = (@"c:\users\woolsvalley\documents\visual studio 2015\Projects\WindowsFormsApplication17\WindowsFormsApplication17\Alarm.wav");
    if ((DateTime.Now.ToString("hh:mm tt") == ((textBox_ordertostart.Text))))
    {
        mplayer.Play();
        pbuilder.ClearContent();

        pbuilder.AppendText("Order time for" + "" + comboBox_suburb.SelectedValue + "Start in" + 5 + "minutes");
        txt2speech.Speak(pbuilder);
    }
}

1 个答案:

答案 0 :(得分:2)

您可以从TimeSpan中减去DateTime,如下所示:

DateTime fiveMinutesAgo = DateTime.Now - new TimeSpan(0,5,0);