将datetime转换为另一种格式 [英] Convert datetime into another format

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

问题描述

我在VB中有以下代码。我在DateTime中有一个变量,我试图将返回值的格式从数据库转换为yyyy-MM-dd HH:mm:ss我试图将其转换为字符串,以便我可以改变其格式但似乎不是按预期工作。以下是我的代码:



Hi I have the following codes in VB. I have a variable in DateTime and I tried to convert the format of returned value from database into yyyy-MM-dd HH:mm:ss I have tried to convert it into string so that I may alter its format but seems like it does not work as expected. Below is my codes:

Dim woStartDate As DateTime
While readerSQL.Read()
      If Not IsDBNull(readerSQL.Item("WOStartDate")) Then
         woStartDate = readerSQL.Item("WOStartDate") '11/8/2016 1.52 PM
         woStartDate = woStartDate.ToString("yyyy-MM-dd HH:mm:ss") '2016-11-08 13:52:21(expected result)
      Else
         woStartDate = DateTime.Now
      End If
End While







价值留在转换后的原始格式相同。我已经多次使用过这个功能(在其他程序中)并且转换似乎没有任何问题。



我尝试了什么:



1.尝试将readerSQL.Item(WOStartDate)直接转换为字符串但返回错误。




The value stay in the same original format after conversion. I have used this function plenty of times(in other program) and it seems not have any problem converting.

What I have tried:

1. Tried to convert readerSQL.Item("WOStartDate") into string directly but returns error.

推荐答案

尝试使用它:



try to use this :

woStartDate  =  DateTime.ParseExact(woStartDate , "MM/dd/yyyy h:mm:ss tt",
           Globalization.CultureInfo.InvariantCulture)


对不起......我用错误的方式阅读了代码。

试试这段代码:



Sorry ... I read the code in the wrong way.
Try this code :

Dim woStartDate As DateTime
dim myStartDate as string
While readerSQL.Read()
      If Not IsDBNull(readerSQL.Item("WOStartDate")) Then
         woStartDate = readerSQL.Item("WOStartDate") '11/8/2016 1.52 PM
         myStartDate = woStartDate.ToString("yyyy-MM-dd HH:mm:ss") '2016-11-08 13:52:21(expected result)
      Else
         myStartDate = DateTime.Now.tostring
      End If
End While





AD ate或DateTime就是这个。

只有一个字符串可以保存不同的格式...



A Date or a DateTime is simply this.
Only a string could hold a different formatting ...


一个DateTime变量没有格式,它代表一个时间点。只有在将其转换为字符串时才会获得格式。当您只是要求DateTime变量将其自身显示为字符串时,它会选择在操作系统的系统设置中定义的格式,如果您希望以特定格式使用,则需要通过ToString提供该格式。 DateTime变量本身虽然没有格式,但是当你显示它时你真正看到的是它的默认表示。



基本上,你不能给DateTime一种格式,你不能用SQL格式给出日期,只有当你将它们转换为字符串时才给出日期格式,所以保留原样并在需要显示它们时转换它们。
A DateTime variable doesn't have a format, it represents a point in time. It only gains a format when you convert it to a string. When you simply ask a DateTime variable to show itself as a string it chooses the format defined in the system settings of the OS, if you want it in a specific format you need to supply that format via ToString. The DateTime variable itself though doesn't have a format, what you are really seeing when you show it is its default representation.

Basically, you can't give DateTime a format, you can't give dates in SQL a format, you only give dates a format when you convert them to string so leave them as they are and convert them when you need to display them.


这篇关于将datetime转换为另一种格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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