将任何时间值转换为hh:mm:ss tt格式 [英] Convert any time value to hh:mm:ss tt format

查看:802
本文介绍了将任何时间值转换为hh:mm:ss tt格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据表中的日期时间值为 29/07/2014 6:07:05 PM 。但我想将datetime值转换为 29/07/2014 06:07:05 PM 。如何转换它?

当我尝试使用

I am getting a datetime value in my datatable as 29/07/2014 6:07:05 PM.But i want to convert datetime value to 29/07/2014 06:07:05 PM. How to convert it?
when i try to convert it using

DateTime.ParseExact(29/07/2014 6:07:05 PM, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).ToString();



它给出异常:字符串无法识别为有效日期时间

请帮助。


it gives exception : String not recognised as valid datetime
Please help.

推荐答案

试试这个:
string a=DateTime.ParseExact("29/07/2014 6:07:05 AM", "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy hh:mm:ss tt");





这给你的例外是因为ParseExact要求转换字符串的格式,而不是要将其转换为的格式。这是在ToString方法中完成的。



The exception this was giving to you is because ParseExact expects the format of the string to convert,not the format you want to convert it to. That is done in the ToString method.


1。我可能是盲人但你在第一句中列出的日期格式没有区别。

2. DateTime.ParseExact()首先要求一个字符串,你在29/07/2014 6中传递: 07:05 PM,这不是一个字符串。你需要把它放在引号中才能成为一个字符串,29/07/2014 6:07:05 PM
1. I could be blind but there is no difference between the date formats you listed in your first sentence.
2. DateTime.ParseExact() expects a string first and you passed in 29/07/2014 6:07:05 PM, which is not a string. You need to put it in quotes for it to be a string, "29/07/2014 6:07:05 PM"


这篇关于将任何时间值转换为hh:mm:ss tt格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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