2010-06-02 106 views
0

我有一个从日期转换为字符串的字符串,数据如下所示:6/2/2010 4:30:00 PM6/2/2010 4:45:00 PM我需要这样的输出:04:30PM。能否请你帮忙?如何提取部分字符串?

+0

该字符串包含两个日期这样呢? – 2010-06-02 12:56:53

回答

1

DateTime.ToString("hh:mmtt")应该做的正是你需要的。

+0

thanx很多它正在工作 – user354547 2010-06-02 13:16:56

0

检查出DateTime.ToShortTimeString方法。请注意,输出格式基于为调用线程设置的CultureInfo

+0

这将在时间和AM/PM之间留出空间。 – 2010-06-02 12:57:53

+0

thanx很多:-) DateTime.ToString(“hh:mmtt”)本身工作正常.... – user354547 2010-06-02 13:28:41

0

使用DateTime.Parse()DateTime.TryParse()解析输入,并使用格式选项DateTime.ToString()spoulson's post一样。

+0

thanx很多:-) – user354547 2010-06-02 13:29:08

-1
DateTime date = new DateTime(2010,2,6,16,30,00); 
date.ToString("hh:mm tt") 
+0

thanx它工作正常:-) – user354547 2010-06-02 13:30:01

+0

我错过了downvote的原因 – RvdK 2010-06-02 13:33:07

+0

你说的行可能不工作,实际上不工作。 – 2010-06-02 13:40:31

0
string orig = "6/2/2010 4:30:00 PM6/2/2010 4:45:00 PM" 
string first = orig.Substring(0, orig.IndexOf("M") + 1); 
DateTime firstDate = DateTime.ParseExact(first, "M/d/yyyy h:mm:ss tt", null); 
string firstTime = firstDate.ToString("hh:mmtt"); 
+0

雅其工作很好.. thanx很多:-) – user354547 2010-06-02 13:19:51