以24小时格式将字符串转换为日期 [英] converting string to date in 24 hr format

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

问题描述

如何在vb.net中将字符串转换为保留24小时格式的日期?

我使用了date.parse但是它改变了24到12小时的格式!!!

格式输出为字符串....

How to convert string to date keeping 24 hr format in vb.net??
I have used date.parse but it is changing 24 to 12 hr format!!!
And format gives output as string....

推荐答案

格式将始终以字符串形式输出:因为12小时/ 24小时不是天生的属性日期或时间:仅在向用户呈现结果或从用户获得输入时才相关。 DateTime对象不关心12小时或24小时格式,因为它们不使用它们:在内部,它们只是在任意固定时间点之后的几毫秒。



要将DateTime格式化为12或24格式的字符串,只需将相应的格式字符串传递给DateTime.ToString(或String.Format) - 它们在此处列出:格式化显示的日期时间 - 格式字符串描述 [ ^ ] - 如果你不提供格式说明符然后使用当前系统设置,可能是12或14小时,具体取决于用户的偏好。
Format will always give output as a string: because 12 hr / 24 hr is not an innate property of a date or time: it is only relevant when presenting the result to the user, or when getting an input from a user. DateTime objects do not care about 12 hr or 24 hour format, because they don't use them: internally they are just a number of milliseconds since an arbitrary fixed point in time.

To format a DateTime as a string with 12 or 24 format, just pass the appropriate format string to DateTime.ToString (or String.Format) - they are listed here: Formatting a DateTime for display - format string description[^] - if you don't supply a format specifier then the current system setting is used, which may be 12 or 14 hour, depending on the user preference.


DateTime.Parse 生成一个 DateTime 对象,这是一个不知道格式的实体。

如果你的意思是 DateTime.Parse 没有正确地解释输入字符串然后你可能会使用 DateTime.ParseExact [ ^ ]方法,可能指定自定义格式 [ ^ ](看看H表中的HH
DateTime.Parse produces a a DateTime object, that is an entity unaware of formats.
If you meant something like "DateTime.Parse doesn't intepretate correctly the input string" then you probably could use the DateTime.ParseExact[^] method, possibly specifying a custom format[^] (have a look at "H" and "HH" in the table).


您好,



试试这个:



Hi,

Try this:

MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt",null);


这篇关于以24小时格式将字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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