我如何才能改变我的DateTime变量“s”的时间?

DateTime s = some datetime;

当前回答

如果你有一个像2014/02/05 18:19:51这样的DateTime,并且只想要2014/02/05,你可以这样做:

_yourDateTime = new DateTime(_yourDateTime.Year, _yourDateTime.Month, _yourDateTime.Day)

其他回答

//The fastest way to copy time            

DateTime justDate = new DateTime(2011, 1, 1); // 1/1/2011 12:00:00AM the date you will be adding time to, time ticks = 0
DateTime timeSource = new DateTime(1999, 2, 4, 10, 15, 30); // 2/4/1999 10:15:30AM - time tick = x

justDate = new DateTime(justDate.Date.Ticks + timeSource.TimeOfDay.Ticks);

Console.WriteLine(justDate); // 1/1/2011 10:15:30AM
Console.Read();

设定一天的结束:

date = new DateTime(date.Year, date.Month, date.Day, 23, 59, 59);

这难道不能解决你的问题吗?

Dateime dt = DateTime.Now;
dt = dt.AddSeconds(10);

如果你有一个像2014/02/05 18:19:51这样的DateTime,并且只想要2014/02/05,你可以这样做:

_yourDateTime = new DateTime(_yourDateTime.Year, _yourDateTime.Month, _yourDateTime.Day)

这是一种贫民区的方法,但它很有效:)

DateTime dt = DateTime.Now; //get a DateTime variable for the example
string newSecondsValue = "00";
dt = Convert.ToDateTime(dt.ToString("MM/dd/yyyy hh:mm:" + newSecondsValue));