calculate remaining time from current date to selected date and display the result on a label.In my design section i have 1 text box with a calender control and 1 button to submit the selected date in database and 1 label which shows the remaining days .
below is my code.
/*********************************************************/
using MySql.Data.MySqlClient;
using System.Windows.Forms;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Calendar1.Visible = true;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
txtdate.Text = Calendar1.SelectedDate.ToShortDateString();
Calendar1.Visible = false;
}
protected void Button2_Click(object sender, EventArgs e)
{
try
{
string MyConnection2 = "datasource=localhost;port=***;username=***;password=***;DATABASE=ts";
string Query4 = "insert into ts1(DATESELECTED,CURRENTDATE) values('" + this.txtdate.Text + "',NOW());";
DateTime.Now.ToString("dd-MM-yyyy ");
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
MySqlCommand MyCommand2 = new MySqlCommand(Query4, MyConn2);
MySqlDataReader MyReader2;
MyConn2.Open();
MyReader2 = MyCommand2.ExecuteReader(); // Here our query will be executed and data saved into the database.
MessageBox.Show("Save Data");
while (MyReader2.Read())
{
}
MyConn2.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
please suggest ASAP
Loading
Jayakumar BalasubramaniamPosted Sep 2, 2014, 3:20 AM
You mean the date selection is allowed to select the dates in future only right ?
Okay, whatever.. why cant you try with normal date subtraction.
And you can get remain Days, Min,Seconds, Hours.. whatever from that.. Its of calculating timespan elapsed between two dates.
If you selecting future date from current date.
(SelectedDate - DateTime.Now).TotalDays
(SelectedDate - DateTime.Now).TotalMinutes
(SelectedDate - DateTime.Now).TotalSeconds
(SelectedDate - DateTime.Now).TotalMilliSeonds
If you selecting past date to current date
(DateTime.Now- SelectedDate).TotalDays
(DateTime.Now- SelectedDate).TotalMinutes
(DateTime.Now- SelectedDate).TotalSeconds
(DateTime.Now- SelectedDate).TotalMilliSeonds
Jayakumar BalasubramaniamPosted Sep 3, 2014, 3:35 AM
in DB mean..
"insert into ts1(DATESELECTED,CURRENTDATE,REMAINDAYS) values('" + this.txtdate.Text + "',NOW(),DATEDIFF(d, GETDATE(),DATESELECTED);";
this is the extra value i added in ur query
"DATEDIFF(d, GETDATE(),DATESELECTED)"
it will subract two dates and display the value of difference in days.
if u have doubt in ur database create new query
paste this
select DATEDIFF(d, GETDATE(),Getdate()+1)
answer will be 1
am getting difference between today and tomorow
for C# mean..
int remainDays= (Calendar1.SelectedDate- DateTime.Now).TotalDays;
Animesh MishraPosted Sep 3, 2014, 2:09 AM
Animesh MishraPosted Sep 3, 2014, 2:06 AM