将字符串转换为日期时间dd / MM / yyyy hh:mm:ss tt [英] Convert string to Datetime dd/MM/yyyy hh:mm:ss tt

查看:254
本文介绍了将字符串转换为日期时间dd / MM / yyyy hh:mm:ss tt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将这个 7/3/2015 12:40:02 PM 转换为格式为的日期时间dd / MM / yyyy hh: mm:ss tt我已经这样做了:

  BreackEndTime = DateTime.ParseExact(configViewModel。 EndPause,dd / MM / yyyy hh:mm:ss tt,CultureInfo.InvariantCulture); 

但我总是得到


字符串未被识别为有效的DateTime。


解决方案

由于月和日可以有一个数字使用

  BreackEndTime = DateTime.ParseExact(configViewModel.EndPause,d / M / yyyy hh:mm:ss tt,CultureInfo不变文化); 

M自定义格式说明符(示例, d 的作品相似)


M自定义格式说明符表示月份,从
1到12(或13到13个月的日历,从1到13)。
单位数字的格式没有前导零。


更新



由于小时还可以使用一位数字:

  DateTime.ParseExact(7/3/2015 1:52:16 PM,d / M / yyyy h:mm:ss tt,CultureInfo.InvariantCulture);`
/ pre>

...所以d / M / yyyy h:mm:ss tt code>d / M / yyyy hh:mm:ss tt。请注意,这同样适用于分钟和秒,如果它们也可以使用单个数字,则使用d / M / yyyy h:m:s tt。我希望你现在得到点。


How can I convert this 7/3/2015 12:40:02 PM to DateTime with format "dd/MM/yyyy hh:mm:ss tt" I have done like this:

BreackEndTime = DateTime.ParseExact(configViewModel.EndPause, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

But I always get

String was not recognized as a valid DateTime.

解决方案

Since months and days can have a single digit use

BreackEndTime = DateTime.ParseExact(configViewModel.EndPause, "d/M/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

The "M" Custom Format Specifier (exemplary, d works similar)

The "M" custom format specifier represents the month as a number from 1 through 12 (or from 1 through 13 for calendars that have 13 months). A single-digit month is formatted without a leading zero.

Update

Since the hour can also have a single digit you have to use:

DateTime.ParseExact("7/3/2015 1:52:16 PM", "d/M/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);` 

... so "d/M/yyyy h:mm:ss tt" instead of "d/M/yyyy hh:mm:ss tt". Note that the same applies to the minutes and seconds, if they also can have single digits use "d/M/yyyy h:m:s tt". I hope you got the point now.

这篇关于将字符串转换为日期时间dd / MM / yyyy hh:mm:ss tt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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