我的日期显示为2011年10月18日3:12:33c#只显示DateTime的时间部分
如何获取此日期时间的时间部分?
我正在使用C#。
我想:
string timeval = PgTime.ToShortTimeString();
,但没有工作的智能感知仅表现的ToString();
我的日期显示为2011年10月18日3:12:33c#只显示DateTime的时间部分
如何获取此日期时间的时间部分?
我正在使用C#。
我想:
string timeval = PgTime.ToShortTimeString();
,但没有工作的智能感知仅表现的ToString();
假设
DateTime PgTime;
您可以:
String timeOnly = PgTime.ToString("t");
其他格式选项can be viewed on MSDN。
另外,如果你想它在一个较大的字符串相结合,则可以执行:
// Instruct String.Format to parse it as time format using `{0:t}`
String.Format("The time is: {0:t}", PgTime);
// pass it an already-formatted string
String.Format("The time is: {0}", PgTime.ToString("t"));
如果PgTime
是TimeSpan
,你有a few other options:
TimeSpan PgTime;
String formattedTime = PgTime.ToString("c"); // 00:00:00 [TimeSpan.ToString()]
String formattedTime = PgTime.ToString("g"); // 0:00:00
String formattedTime = PgTime.ToString("G"); // 0:00:00:00.0000000
另外http://msdn.microsoft.com/en-我们/库/ system.datetime.toshorttimestring(v = VS.100)的.aspx – asawyer 2012-01-03 20:42:03
如果您想要一个格式化的字符串,只需使用.ToString(format)
,只指定时间部分。如果你想要的实际时间,使用.TimeOfDay
,这将从午夜的TimeSpan
。
DateTime test = DateTime.Now;
MessageBox.Show(test.ToShortTimeString());
只产生DateTimne值的时间部分。 你能告诉我们如何声明PgTime吗?
DateTime PgTime = new DateTime();
var hr = PgTime.Hour;
var min = PgTime.Minute;
var sec = PgTime.Second;
//or
DateTime.Now.ToString("HH:mm:ss tt") gives it to you as a string.
现在不要关于名为PgTime
的类。尽管现在要做DateTime
。
尝试
DateTime instance = DateTime.Now ; // current date/time
string time = instance.ToString("t") ; // short time formatted according to the rules for the current culture/locale
可能要在Standard Date and Time Format Strings和Custom Date and Time Format Strings
什么是'PgTime'读了? – David 2012-01-03 20:40:52
什么数据类型是PgTime? – DaveShaw 2012-01-03 20:41:10
你没有给我们足够的上下文。什么是编译时间类型的'PgTime'?您使用该代码时遇到了哪些编译器错误?请阅读http://tinyurl.com/so-hints – 2012-01-03 20:41:15