C#和日期文化问题 [英] c# and Date Culture Problems

查看:125
本文介绍了C#和日期文化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了asp.net应用程序,我回发程序中的一个简单的用户提交的表单数据保存到SQL 2005分贝。所有伟大的运行在我的机器,但是当我部署到直播现场我越来越从我解析日期检查无效的日期。

I've written a asp.net app and one of my postback routines simply saves user submitted form data to a sql 2005 db. All runs great on my development machine but when I deploy to the live site I'm getting invalid dates from my parse date checker.

基本上,它期待在现场机器上的一个美国的日期格式,但是这不是我想要的。用户需要能够在DD / MM / YYYY格式输入。所以像21/10/2009有效的日期返回直播服务器,但不是我的开发机器的错误。下面是code抛出异常。

Basically it is expecting an american date format on the live machine but this is not what I want. The user needs to be able to enter in dd/MM/yyyy format. So a valid date like 21/10/2009 returns errors on live server but not on my dev machine. Below is the code that throws the exception.

DateTime dt;
dt = DateTime.Parse(sdate);  
//sdate in GB dd/MM/yyyy format

是否有可能迫使解析程序期望在DD日/ MM / YYYY格式?

Is it possible to force the parse routine to expect the date in dd/MM/yyyy format?

推荐答案

这样做:

    System.Globalization.CultureInfo cultureinfo = 
        new System.Globalization.CultureInfo("en-gb");
    DateTime dt = DateTime.Parse("13/12/2009", cultureinfo);

这篇关于C#和日期文化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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