字符串覆盖时间 [英] coversion of string to time

查看:111
本文介绍了字符串覆盖时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想在数据库中添加时间,我想从用户那里获取时间,我已经将3个下拉列表放在第一位(每小时),第二位(分钟)和第三位(上午/下午)..
现在如何将其转换为时间变量以将其存储在数据库中.


有人帮助我...

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...

推荐答案

嗨 你可以做

Hi You can do

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();



如有任何疑问,请让我知道.

请提供"投票":thumbsup:如果有帮助,请提供"接受答案",如果这是正确的答案.:rose:

谢谢,
Imdadhuse
n



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


您可以将其存储为DateTime并忽略日期部分.
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;

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);


最终结果是,您有一个名为timeValue的DateTime变量,它具有当前日期和输入的时间,您可以将整个值存储在数据库中的DateTime字段中,然后在检索它时,可以只查看小时和分钟.属性.

如果您不想存储日期部分而只想存储时间,请使用类似的内容;


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;


获取组成时间部分的刻度数并将其存储在数据库中long类型的字段中.如果从数据库中获取值时执行此操作,则会像这样加载它;


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);


当您执行此操作时,日期部分将变为01/01/0001;


When you do this the date portion becomes 01/01/0001;


正如Abhijith所说,请使用DateTime.ParseExact().在此之前,您需要连接3个DropdownList.
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);


这篇关于字符串覆盖时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆