C# 关於DateTime的问题
这是预约房间的系统
Book Time是想预约的时间
Due Time是房间到期的时间
预约房间的使用期为1小时
在图中 我选了预约12点 房间到期的时间该是1点吧
为何在ListView显示的时间会不正确? --------------------编程问答-------------------- private void Booking_Load(object sender, EventArgs e)
{
bookTime.Format = DateTimePickerFormat.Custom;
bookTime.CustomFormat = "MM dd yyyy hh mm";
bookTime.Value = DateTime.Now;
dueTime.Format = DateTimePickerFormat.Custom;
dueTime.CustomFormat = "MM dd yyyy hh mm";
dueTime.Value = DateTime.Now; --------------------编程问答-------------------- private void dueDate_ValueChanged(object sender, EventArgs e)
{
//check dueTime//
dueTime.Value = bookTime.Value.AddMinutes(60);
} --------------------编程问答-------------------- private void btnConfirm_Click(object sender, EventArgs e)
{
/////confirm booking///
txtBookNo.ReadOnly = true;
string bookNo = txtBookNo.Text;
string sqlComfirm;
OleDbCommand cmd;
sqlComfirm = "INSERT INTO Book ("
+ "BookNo, "
+ "BookTime, "
+ "memberNo "
+ ") VALUES ("
+ toSql(int.Parse(txtBookNo.Text)) + ", "
+ toSql(bookTime.Value.ToLocalTime()) + ", "
+ toSql(memberNoComboBox.Text).ToString()
+ ")";
try
{
cmd = new OleDbCommand(sqlComfirm, mDB);
cmd.ExecuteNonQuery();
MessageBox.Show("Booking inserted successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
btnBook.Enabled = true;
} --------------------编程问答-------------------- dueTime.Format = DateTimePickerFormat.Custom;
dueTime.CustomFormat = "MM dd yyyy hh mm";
dueTime.Value = DateTime.Now;
你这里过期时间这样写是不对的,你这样写,过期时间只会保存了当前的时间,你是想保存预定时候的时间,你可以用一个临时变量来保存预定时候的时间,比如string DatetimeCahe=DateTime.Now.ToString().然后单击保存按钮的时候,可以用Convert.ToDateTime(DatetimeCahe).Value.AddMinutes(60);
--------------------编程问答--------------------
不明白
+ toSql(bookTime.Value.ToLocalTime()) + ", "
这句要如何改 --------------------编程问答--------------------
改为+ toSql(Convert.ToDateTime(DatetimeCahe)) + ", "
还有就是bookTime.Format = DateTimePickerFormat.Custom;
bookTime.CustomFormat = "MM dd yyyy hh mm";
bookTime.Value = DateTime.Now; //
改为
bookTime.Format = DateTimePickerFormat.Custom;
bookTime.CustomFormat = "MM dd yyyy hh mm";
bookTime.Value = Convert.ToDateTime(DatetimeCahe) --------------------编程问答-------------------- 不管你是预定时间 还是过期时间,你用一个临时时间变量来保存就可以了。
然后插入数据库的时候就用这个临时变量保存的时间作为预定时间,过期时间就在这个临时变量上面加60分钟就好了 --------------------编程问答--------------------
string DateTimeCahe="";//全局变量
private void dueDate_ValueChanged(object sender, EventArgs e)
{
//check dueTime//
DateTimeCahe=DateTime.Now.ToString();
dueTime.Value = Convert.ToDateTime(DatetimeCahe).Value.AddMinutes(60);
}
;
private void btnConfirm_Click(object sender, EventArgs e)
{
/////confirm booking///
txtBookNo.ReadOnly = true;
string bookNo = txtBookNo.Text;
string sqlComfirm;
OleDbCommand cmd;
sqlComfirm = "INSERT INTO Book ("
+ "BookNo, "
+ "BookTime, "
+ "memberNo "
+ ") VALUES ("
+ toSql(int.Parse(txtBookNo.Text)) + ", "
+ toSql(Convert.ToDateTime(DatetimeCahe).Value)+ ", "
+ toSql(memberNoComboBox.Text).ToString()
+ ")";
try
{
cmd = new OleDbCommand(sqlComfirm, mDB);
cmd.ExecuteNonQuery();
MessageBox.Show("Booking inserted successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
btnBook.Enabled = true;
}
补充:.NET技术 , C#