Hi i have two values of type dateTime

DateTime dateOfEvent = Convert.ToDateTime(dtpReservationDate.Value.ToLongDateString());
 DateTime timeOfEvent = Convert.ToDateTime(dtpEventTime.Value.ToLongTimeString());

now i would like to assign these date and time to one variable of datatype dateTime. How can i do so?

DateTime dateTimeOfEvent = ??????

Recommended Answers

All 4 Replies

Lets clear some things now, and lets look into your example code:

DateTime dateOfEvent = Convert.ToDateTime(dtpReservationDate.Value.ToLongDateString());

You dont need to assign a method "ToLongDateString()", because you want to get a real DateTime value. And real dateTime value has nothing to do with the string date (or string time).
It would be enough to write:

DateTime dateOfEvent = dtpReservationDate.Value; //if this is some kind of dateTimePicker or monthCalendar
//if not, you do:
DateTime dateOfEvent = Convert.ToDateTime(dtpReservationDate.Value); //this should do it in the worst case scenario

... so you want now what exactly? I didnt get your question.
If you only want to pass the variable to another field or propery you can do:

DateTime dateTimeOfEvent = dateOfEvent;

Because every real DateTime value will have all (date and time). If this will not be possible to convert, you will get an error before that (in the upper code you pasted - for example).

Lets clear some things now, and lets look into your example code:

DateTime dateOfEvent = Convert.ToDateTime(dtpReservationDate.Value.ToLongDateString());

You dont need to assign a method "ToLongDateString()", because you want to get a real DateTime value. And real dateTime value has nothing to do with the string date (or string time).
It would be enough to write:

DateTime dateOfEvent = dtpReservationDate.Value; //if this is some kind of dateTimePicker or monthCalendar
//if not, you do:
DateTime dateOfEvent = Convert.ToDateTime(dtpReservationDate.Value); //this should do it in the worst case scenario

... so you want now what exactly? I didnt get your question.
If you only want to pass the variable to another field or propery you can do:

DateTime dateTimeOfEvent = dateOfEvent;

Because every real DateTime value will have all (date and time). If this will not be possible to convert, you will get an error before that (in the upper code you pasted - for example).

because my date and time are in 2 separate variables.. finally i want to join them.. how?

You have to get strings seperately from that control, and later join both into a dateTime:

string a = tpReservationDate.Value.ToLongDateString();
string b = tdtpEventTime.Value.ToLongTimeString();
DateTime date = Convert.ToDateTime(a + " " + b);

//Example:         
string a = "13.3.2011";
string b = "13:54:32";
DateTime date = Convert.ToDateTime(a + " " + b);

thank u very much for ur help it worked :).. I was doing the same as u tutored but i didnt put any space in d string thx!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.