Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey i want to add time in database , i want to get time from user i had put 3 dropdownlist 1-st for hour 2-nd for minutes and 3-rd for am/pm..
now how can i convert it into time variable to store it in database.


somebody help me...
Posted

Hi You can do

C#
string strHour = "10";      //You can set Hour Dropdown Selected value
string strMinutes = "45";   //You can set Minutes Dropdown Selected value
string strType = "AM";      //You can set Type Dropdown Selected value
string strDate = DateTime.Now.ToString("dd/MM/yyyy");
string strFormat = strDate + " " + strHour + ":" + strMinutes + ":00 " + strType;
DateTime finalDateTime = DateTime.ParseExact(strFormat, "dd/MM/yyyy hh:mm:ss tt", null);
string strTime = finalDateTime.TimeOfDay.ToString();


Please do let me know, if you have any doubt.

Please provide "Vote":thumbsup: if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhuse
n
 
Share this answer
 
Comments
Sunasara Imdadhusen 3-Nov-10 2:28am    
This answer is Tested and it is working as per your requirement. you can try this solution if you would like?
rakesh2577 3-Nov-10 2:41am    
but if i not want to add date i want to add time only..
You can store it as a DateTime and ignore the date portion.
DateTime.ParseExact("1:45 pm", "h:mm tt", null)

Using this the date portion will default to whatever the current date is.

** Update **
Bit more of an explanation.
Lets say you have;
C#
string hourValue = "1";
string minuteValue = "45";
string timePeriod = "PM";
string timeString = string.Format("{0}:{1} {2}", hourValue, minuteValue, timePeriod);
DateTime timeValue = DateTime.ParseExact(timeString, "h:mm tt", null);

End result is you have a DateTime variable called timeValue this has the current date and the time that was entered, you can store this entire value in the database in a DateTime field then when you retrieve it you can either just look at the Hour and Minute properties.

If you don't want to store the date portion and just store the time use something like;
long timeTicks = timeValue.TimeOfDay.Ticks;

To get the number of Ticks that make up the time portion and store that in the database in a field of type long. If you do this when you get the value from the database you load it like;
DateTime timeValue = DateTime.FromBinary(timeTicks);

When you do this the date portion becomes 01/01/0001;
 
Share this answer
 
v2
Comments
rakesh2577 3-Nov-10 2:40am    
its working but if i want not to put current date in date portion i want to keep date portion as null. then is there any facility for that.
Rod Kemp 3-Nov-10 2:44am    
You can't have the date portion as null, you can just ignore it.
rakesh2577 3-Nov-10 2:48am    
how??
Sunasara Imdadhusen 3-Nov-10 2:52am    
Good Answer
rakesh2577 3-Nov-10 3:02am    
in database i want to add only time and date as null.
As Abhijith said use DateTime.ParseExact(). Before that you need to concatenate the 3 DropdownLists.
yourstring = DropDownList1.SelectedValue + DropDownList2.SelectedValue + DropDownList3.SelectedValue;
//then 
DateTime newTime= DateTime.Parse(yourstring);
 
Share this answer
 
v2
Comments
Rod Kemp 3-Nov-10 2:40am    
This will error, using DateTime.Parse without a date portion will result in a FormatException error what should be used is DateTime.ParseExact with the time format specified.
m@dhu 3-Nov-10 2:48am    
Updated.
Use DateTime.Parse() for ths same.

DateTime newTime= DateTime.Parse(yourstring);
 
Share this answer
 
Comments
Sunasara Imdadhusen 3-Nov-10 2:38am    
You may not set datetime as per your system format
rakesh2577 3-Nov-10 2:47am    
but if i not want to add date i want to add time only..

and timeformat is h:mm tt as per my system and code too

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900