上午/下午出现的时间 [英] Time to appear with am/pm
本文介绍了上午/下午出现的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
16:03:19.0830000
我将其存储在我的数据库中作为时间(7),我希望时间出现在下午4:03。不是这样的。我使用此方法但失败了,字符串不是有效的日期时间
string timeFromFormat = hh:mm tt;
DateTime timeFrom = DateTime.ParseExact(tb_TimeFrom.Text,timeFromFormat,CultureInfo.InvariantCulture);
解决方案
字符串数据你show is not 一个DateTime:它是一个TimeSpan。要将TimeSpan转换为时间点的可用表示,您必须执行以下操作:private void TestConversion()
{
string dbDataString =16:03:19.0830000;
//匹配数据库中的TimeSpan格式
字符串timeFromFormat =hh \\:mm \\:ss \\\\ ffffffff;
//输出格式
string simpleAMPM ={0:h:mm tt};
//转换为TimeSpan
TimeSpan ts = TimeSpan.ParseExact(dbDataString,timeFromFormat,CultureInfo.InvariantCulture);
//将TimeSpan添加到DateTime
DateTime theDate = new DateTime()+ ts;
//格式化结果
string theTime = string.Format(simpleAMPM,theDate);
//检查输出...
Console.WriteLine(theTime);
}
请尝试如下。
DateTime myDate = DateTime.ParseExact(16:03:19,083,HH:mm:ss,fff,
System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(myDate.ToString(hh:mm tt));
现场演示: ideone
>
string timeFromFormat =hh:mm tt;
错误。
它应该是string timeFromFormat = HH:MM:TT
;
16:03:19.0830000
I store this in my db as time(7) , I want the time to appear in 4:03pm . Not like this. I use this method but failed, string is not a valid datetime
string timeFromFormat = "hh:mm tt";
DateTime timeFrom = DateTime.ParseExact(tb_TimeFrom.Text, timeFromFormat, CultureInfo.InvariantCulture);
解决方案
The string data you show is not a DateTime: it's a TimeSpan. To convert a TimeSpan into a usable representation of a point in time, you have to do something like this:private void TestConversion() { string dbDataString = "16:03:19.0830000"; // match the format of the TimeSpan in the Database string timeFromFormat = "hh\\:mm\\:ss\\.fffffff"; // output format string simpleAMPM = "{0:h:mm tt}"; // convert to TimeSpan TimeSpan ts = TimeSpan.ParseExact(dbDataString, timeFromFormat, CultureInfo.InvariantCulture); // add the TimeSpan to a DateTime DateTime theDate = new DateTime() + ts; // format the result string theTime = string.Format(simpleAMPM, theDate); // check output ... Console.WriteLine(theTime); }
Please try is as below.
DateTime myDate = DateTime.ParseExact("16:03:19,083", "HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture); Console.WriteLine(myDate.ToString("hh:mm tt"));
LIVE DEMO : ideone
string timeFromFormat = "hh:mm tt";
is wrong.
It should bestring timeFromFormat = "hh:mm:tt"
;
这篇关于上午/下午出现的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文