将字符串转换为日期时间 [英] Converting string to date time

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

问题描述

您好,


我正在尝试将字符串转换为C#Web服务中的日期时间。


我'传入一个字符串参数,我在我的Web.config文件中有一个本地化设置:




我的应用程序在第一行爆炸了,价值不会下降

在预期范围内错误。


CultureInfo ci = new CultureInfo(" en-US",true);

ci.DateTimeFormat.ShortDatePattern =

ConfigurationSettings.AppSettings [" DateFormat"];


try

{

DateTime myStartDate = DateTime.Parse(startDate ,ci);

}


知道什么是错的吗?有没有更简单的方法呢?


-Eric

Hello,

I''m trying to convert a string to a date time in a C# web service.

I''m passing in a string parameter and I have a localization setting in
my Web.config file:

My app is blowing up on the first line here, with a"Value does not fall
within the expected range" error.

CultureInfo ci = new CultureInfo("en-US",true);
ci.DateTimeFormat.ShortDatePattern =
ConfigurationSettings.AppSettings["DateFormat"];

try
{
DateTime myStartDate = DateTime.Parse(startDate, ci);
}

Any idea what''s wrong? Is there a simpler way to do this?

-Eric

推荐答案

< er**********@gmail.com>写道:
<er**********@gmail.com> wrote:
我正在尝试将字符串转换为
C#Web服务中的日期时间。 [...]知道什么是错的?


那么,你传递的价值是多少?

有更简单的方法吗?
I''m trying to convert a string to a date time in a
C# web service. [...] Any idea what''s wrong?
Well, what''s the value that you''re passing in?
Is there a simpler way to do this?




为什么你不能传入DateTime而不是字符串?当你必须处理美国以外的地区时,它也不会失败
(例如欧洲和日本,每个都有自己不同的日期
格式)。


Eq。



Why can''t you pass in a DateTime instead of a string? It also won''t
fail when you have to deal with locales outside the United States
(e.g. Europe and Japan, which each have their own different date
formats).

Eq.


你好,


我的网络服务是模拟另一家公司网络服务行为的测试工具,因此我必须使用他们使用的API。

我不认为我通过的事情很重要,我正在传递

" 01.01.06"。我的应用程序给我的价值不会下降

在预期的范围内当我尝试执行第一行时:


CultureInfo ci = new CultureInfo(" en-US",true);


谢谢,

-Eric

Hello,

My web service is a test harness to mimic the behavior of another
company''s web service, so I have to use the API that''s they use.

I don''t think that it matters what I pass through, I''m passing in
"01.01.06". My app is giving me the Value does not fall
within the expected range" when I try to execute the first line:

CultureInfo ci = new CultureInfo("en-US",true);

Thanks,
-Eric


< er ********** @ gmail.com>写道:
<er**********@gmail.com> wrote:
我的网络服务是模拟另一个公司网络服务行为的测试工具,因此我必须使用他们使用的API。 />
我不认为我通过的事情很重要,我正在通过
01.01.06。我的应用程序正在给我的价值不会落在预期的范围内当我尝试执行第一行时:

CultureInfo ci = new CultureInfo(" en-US",true);
My web service is a test harness to mimic the behavior of another
company''s web service, so I have to use the API that''s they use.

I don''t think that it matters what I pass through, I''m passing in
"01.01.06". My app is giving me the Value does not fall
within the expected range" when I try to execute the first line:

CultureInfo ci = new CultureInfo("en-US",true);




那个似乎不太可能。完整堆栈跟踪是什么样的?

您能发布一个简短但完整的程序来演示

问题吗?


请参阅 http://www.pobox.com/~skeet/csharp /complete.html 了解详情

我的意思是什么。

-

Jon Skeet - < sk * **@pobox.com>
http://www.pobox.com/ ~Sibet 博客: http://www.msmvps.com/jon。双向飞碟

如果回复小组,请不要给我发邮件



That seems somewhat unlikely. What does the full stack trace look like?
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


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

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