在 Vb.net 中转换日期时间格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22397856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:04:36  来源:igfitidea点击:

Convert Datetime Format in Vb.net

vb.netvisual-studiodatetime

提问by Denmark

Here is my datetime data in excel

这是我在 excel 中的日期时间数据

3/10/2014  11:59:59 AM

And i want to convert it into like this in vb.net

我想在 vb.net 中将它转换成这样

  // I have a variable that contains the datetime like this

   Dim audit_date as DateTime = 3/10/2014 11:59:59 AM // and i want to convert this to the one below

   Output = 2014-03-10 11:59:59

also i want to remove that AM/PM

我也想删除那个 AM/PM

回答by NullReferenceException

You can use like this

你可以这样使用

Dim res As string=DateTime.Now.Tostring("yyyy-MM-dd hh:mm:ss")

Edit:

编辑:

Dim res As string=Convert.ToDateTime("3/10/2014  11:59:59 AM").Tostring("yyyy-MM-dd hh:mm:ss")

I hope this helps.

我希望这有帮助。

回答by Nagaraj S

If you are converting DateTime to String

如果您要将 DateTime 转换为 String

sDate.ToString("yyyy-MM-dd HH:mm:ss")

or

或者

Dim timeFormat As String = "yyyy-MM-dd HH:mm:ss"

SDate.ToString(timeFormat)