Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DateTimePicker Comparisiom with datetimevariable
 

DateTimePicker Comparisiom with datetimevariable

Hi,
I have one datetime variable and when i click on datetime picker i want to compare both values as equal..

datetime variable is dt;
datetimepicker1.value==dt;
but it has seconds difference so can any one help me in this regard please..

thanks,
Kumar..
kumar
kumar449  Thursday, March 26, 2009 12:28 PM
If they have different seconds, they are different, but if you want to omit seconds in the comparison you should go for:

bool equal = dt.AddSeconds(-dt.Seconds) ==datetimepeicker1.value.AddSeconds(-datetimepeicker1.value.Seconds)
Christoph Wagner  Friday, March 27, 2009 6:51 AM

Hi Kumar,

If you only want to compare the Year, Month and Day part of two DateTime value, you can opt to the following two methods except the way Christoph has suggested.

1. Construct new DateTime values based on the Year, Month and Day parts. For example:

DateTime dt = DateTime.Now;           
DateTime dt1 = new DateTime(dt.Year, dt.Month, dt.Day);
DateTime dt2 = new DateTime(this.dateTimePicker1.Value.Year, this.dateTimePicker1.Value.Month, this.dateTimePicker1.Value.Day);
if (dt1 == dt2)
{
   ....
}       
2. Compare the Year, Month and Day parts of the two DateTime values respectively. For example:

DateTime dt = DateTime.Now;           
if (dt.Year == this.dateTimePicker1.Value.Year && dt.Month == this.dateTimePicker1.Value.Month && dt.Day == this.dateTimePicker1.Value.Day)
{
   ...
}

Hope this helps.
Linda Liu  Tuesday, March 31, 2009 6:57 AM
If they have different seconds, they are different, but if you want to omit seconds in the comparison you should go for:

bool equal = dt.AddSeconds(-dt.Seconds) ==datetimepeicker1.value.AddSeconds(-datetimepeicker1.value.Seconds)
Christoph Wagner  Friday, March 27, 2009 6:51 AM

Hi Kumar,

If you only want to compare the Year, Month and Day part of two DateTime value, you can opt to the following two methods except the way Christoph has suggested.

1. Construct new DateTime values based on the Year, Month and Day parts. For example:

DateTime dt = DateTime.Now;           
DateTime dt1 = new DateTime(dt.Year, dt.Month, dt.Day);
DateTime dt2 = new DateTime(this.dateTimePicker1.Value.Year, this.dateTimePicker1.Value.Month, this.dateTimePicker1.Value.Day);
if (dt1 == dt2)
{
   ....
}       
2. Compare the Year, Month and Day parts of the two DateTime values respectively. For example:

DateTime dt = DateTime.Now;           
if (dt.Year == this.dateTimePicker1.Value.Year && dt.Month == this.dateTimePicker1.Value.Month && dt.Day == this.dateTimePicker1.Value.Day)
{
   ...
}

Hope this helps.
Linda Liu  Tuesday, March 31, 2009 6:57 AM
Thanks for that and Sorry for the delay in reply...
fine I understand..I need one more suggestion like how can i current day,month,year dynamically on respective day with out creating date object...Please do Help me in This..
kumar
kumar449  Tuesday, April 21, 2009 5:15 PM
Thanks for that and Sorry for the delay in reply...
fine I understand..I need one more suggestion like how can i current day,month,year dynamically on respective day with out creating date object...Please do Help me in This..
kumar
kumar449  Tuesday, April 21, 2009 5:16 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView (VB.net) problems, nonvisible rows are still visible or the DataGridView is empty despite having a DataSource
• Master Details
• DataGrid Question - Using a column in a selected row - Newbie
• DataGridCell, EditMode and DataSourcechanges?
• Hiding a column's right border
• SQL table Column autoIncrement problem??
• DataSet dissapears
• Using ObjectDataSource with DataGridView in a Windows Form
• What is the best way to load an excel file or cvs file into a dataset...
• Order/OrderDetail insert problem!!!