将格式为12小时(04/29/2013 10:00:00 AM)的字符串转换为datetime in C# [英] Convert string with format 12 hour(04/29/2013 10:00:00 AM) to datetime in C#

查看:409
本文介绍了将格式为12小时(04/29/2013 10:00:00 AM)的字符串转换为datetime in C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的config.xml中检索这样的字符串(04/29/2013 10:00:00 AM)。我想将其转换为DateTime格式。如何做?



我试过以下:

  DateTime dtNextrunTimeforExcel = Convert.ToDateTime 

  DateTime dtNextrunTimeforExcel = DateTime.Parse(str); 

但是,在这种情况下,其说法字符串未被识别为有效的datetime p>

注意:我使用VS2010编写Windows服务



帮助。

解决方案

尝试使用 .ParseExact

  var dtNextrunTimeforExcel = DateTime.ParseExact(str,MM / dd / yyyy hh:mm:ss,CultureInfo.CurrentCulture); 


I'm retrieving a string like this (04/29/2013 10:00:00 AM) from my config.xml.I want to convert this to DateTime format.How to do this?

I tried followings:

DateTime dtNextrunTimeforExcel = Convert.ToDateTime(str); 

and

DateTime dtNextrunTimeforExcel = DateTime.Parse(str);

But, in both the case its saying "string was not recognized as a valid datetime".

Note:I'm writing Windows service with VS2010

Pl. help.

解决方案

Try using .ParseExact

var dtNextrunTimeforExcel = DateTime.ParseExact(str, "MM/dd/yyyy hh:mm:ss", CultureInfo.CurrentCulture);

这篇关于将格式为12小时(04/29/2013 10:00:00 AM)的字符串转换为datetime in C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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