在Windows应用程序中接受所有类型的日期时间 [英] Accept All Kind of Datetime in Windows Application

查看:51
本文介绍了在Windows应用程序中接受所有类型的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在Windows应用程序中工作。我正在使用日期时间概念开发基于c#.net的简单通知系统。这个项目在我的系统中工作。我需要将datetime验证为dd / MM / yyyy,而在客户端系统错误中验证时,字符串不会被识别为有效的日期时间。 becoz客户端系统有系统格式:dd MMMM yyyy



让我知道如何在我的项目中接受所有类型的日期时间格式..



二手代码



Hi,

i'm Working in Windows Application. I'm Developing Simple Notification System based c#.net with datetime concept. this project working in my system. i need validate datetime as dd/MM/yyyy while validate in client system error occur as string not recognized as a valid datetime. becoz client system have system formatted : dd MMMM yyyy

let me know how to accept all kind of datetime format in my project..

Used Code

public bool DateValidation(string stringDateValue)
        {
            bool A = false;
            try
            {
                CultureInfo CultureInfoDateCulture = new CultureInfo("fr-FR");
                DateTime d = DateTime.ParseExact(stringDateValue, "dd/MM/yyyy", CultureInfoDateCulture);
                A = true;
            }
            catch
            {
               
                        A = false;
            }
        

            return A;
        }

推荐答案

不要假设任何格式:只使用当前格式,这应该是用户输入的格式(因为这是他喜欢使用的):

Don't assume any format: just use the current format and that should be what the user entered (since that's what he likes to use):
public bool DateValidation(string stringDateValue)
    {
    DateTime d;
    return DateTime.TryParse(stringDateValue, out d);
    }

或者更好的是,使用DateTimePicker并且您不需要将其验证为有效的DateTime,因为用户无法输入错误的。

Or better still, use a DateTimePicker and you don't need to validate it as a valid DateTime as the user can't enter a "bad" one.


DateTime d = DateTime.ParseExact(stringDateValue,  new string[] { "dd/MM/yyyy", "MM/dd/yyyy", "dd MMMM yyyy" }, CultureInfoDateCulture, DateTimeStyles.None);


这篇关于在Windows应用程序中接受所有类型的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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